1128. Number of Equivalent Domino Pairs - Easy

南笙酒味 提交于 2019-11-26 23:19:36

Given a list of dominoesdominoes[i] = [a, b] is equivalentto dominoes[j] = [c, d] if and only if either (a==c and b==d), or (a==d and b==c) - that is, one domino can be rotated to be equal to another domino.

Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].

 

Example 1:

Input: dominoes = [[1,2],[2,1],[3,4],[5,6]]
Output: 1

 

Constraints:

  • 1 <= dominoes.length <= 40000
  • 1 <= dominoes[i][j] <= 9

 

hash table (naive): 把一个domino pair存入set,再作为key存入map,遍历dominoes,计数每个set出现多少次,最后再计算组合数求pair num

time: O(n), space: O(n)

class Solution {
    public int numEquivDominoPairs(int[][] dominoes) {
        Map<Set<Integer>, Integer> map = new HashMap<>();
        for(int[] domino : dominoes) {
            Set<Integer> set = new HashSet<>();
            set.add(domino[0]);
            set.add(domino[1]);
            
            map.put(set, map.getOrDefault(set, 0) + 1);
        }
        
        int res = 0;
        for(Map.Entry<Set<Integer>, Integer> entry: map.entrySet()) {
            int n = entry.getValue();
            if(n >= 2) {
                res += n * (n - 1) / 2;
            }
        }
        return res;
    }
}

 

optimized: 把一个domino pair处理成一个两位的数字,再作为key存入map

time: O(n), space: O(n)

class Solution {
    public int numEquivDominoPairs(int[][] dominoes) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int[] domino : dominoes) {
            int max = Math.max(domino[0], domino[1]);
            int min = Math.min(domino[0], domino[1]);
            int n = min * 10 + max;
            map.put(n, map.getOrDefault(n, 0) + 1);
        }
        
        int res = 0;
        for(Map.Entry<Integer, Integer> entry: map.entrySet()) {
            int n = entry.getValue();
            if(n >= 2) {
                res += n * (n - 1) / 2;
            }
        }
        return res;
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!