zoukankan      html  css  js  c++  java
  • 1128. Number of Equivalent Domino Pairs

    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;
        }
    }
  • 相关阅读:
    vue_源码 原理 剖析
    vue_vuex
    vue_VueRouter 路由_路由器管理n个路由_并向路由组件传递数据_新标签路由_编程式路由导航
    vue_mint-ui
    vue_ajax 请求
    vue_组件间通信:自定义事件、消息发布与订阅、槽
    vue_小项目_吃饭睡觉打豆豆
    vue-cli 脚手架 Command Line Interface
    程序员面试金典-面试题 04.03. 特定深度节点链表
    程序员面试金典-面试题 04.02. 最小高度树
  • 原文地址:https://www.cnblogs.com/fatttcat/p/11334969.html
Copyright © 2011-2022 走看看