In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)

We may rotate the i-th domino, so that A[i] and B[i] swap values.

Return the minimum number of rotations so that all the values in A are the same, or all the values in B are the same.

If it cannot be done, return -1.

Example 1:

  1. Input: A = [2,1,2,4,2,2], B = [5,2,6,2,3,2]
  2. Output: 2
  3. Explanation:
  4. The first figure represents the dominoes as given by A and B: before we do any rotations.
  5. If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.

Example 2:

  1. Input: A = [3,5,1,2,3], B = [3,6,3,3,4]
  2. Output: -1
  3. Explanation:
  4. In this case, it is not possible to rotate the dominoes to make one row of values equal.

Note:

  1. 1 <= A[i], B[i] <= 6
  2. 2 <= A.length == B.length <= 20000

Approach #1: [Java]

  1. class Solution {
  2. public int minDominoRotations(int[] A, int[] B) {
  3. int n = A.length;
  4. for (int i = 0, a = 0, b = 0; i < n && (A[i] == A[0] || B[i] == A[0]); ++i) {
  5. if (A[i] != A[0]) a++;
  6. if (B[i] != A[0]) b++;
  7. if (i == n-1) return Math.min(a, b);
  8. }
  9. for (int i = 0, a = 0, b = 0; i < n && (A[i] == B[0] || B[i] == B[0]); ++i) {
  10. if (A[i] != B[0]) a++;
  11. if (B[i] != B[0]) b++;
  12. if (i == n-1) return Math.min(a, b);
  13. }
  14. return -1;
  15. }
  16. }

  

Analysis:

1. try A[0]

2. try B[0]

3. return -1;

one observation is that if A[0] and B[0] are all work, the result will be the same.

Approach #2. HashSet. [Java]

  1. class Solution {
  2. public int minDominoRotations(int[] A, int[] B) {
  3. int n = A.length;
  4. HashSet<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));
  5. int[] countA = new int[7], countB = new int[7];
  6. for (int i = 0; i < n; ++i) {
  7. set.retainAll(new HashSet<>(Arrays.asList(A[i], B[i])));
  8. countA[A[i]]++;
  9. countB[B[i]]++;
  10. }
  11. for (int s : set) return Math.min(n - countA[s], n - countB[s]);
  12. return -1;
  13. }
  14. }

  

Analysis:

Find intersection s of {A[i], B[i]}.

s.size() == 0: no possible reslut.

s.size() == 1: one and only one reslut.

s.size() == 2: it means all dominoes are [a, b] or [b, a], try either one.

s.size() > 2: impossible.

Reference:

https://www.geeksforgeeks.org/arrays-aslist-method-in-java-with-examples/

https://www.geeksforgeeks.org/set-retainall-method-in-java-with-example/

https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/discuss/252242/JavaC%2B%2BPython-Different-Ideas

1007. Minimum Domino Rotations For Equal Row的更多相关文章

  1. 【leetcode】1007. Minimum Domino Rotations For Equal Row

    题目如下: In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  ( ...

  2. 【LeetCode】1007. Minimum Domino Rotations For Equal Row 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历一遍 日期 题目地址:https://leetc ...

  3. [LC] 1007. Minimum Domino Rotations For Equal Row

    In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  (A domi ...

  4. [Swift]LeetCode1007. 行相等的最少多米诺旋转 | Minimum Domino Rotations For Equal Row

    In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  (A domi ...

  5. Minimum Domino Rotations For Equal Row LT1007

    In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  (A domi ...

  6. Leetcode: Minimum Domino Rotations For Equal Row

    In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino. (A domin ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)

    Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...

  9. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

随机推荐

  1. springmvc使用数组接收页面商品列表批量删除传过来的参数,并完成批量删除的操作。

    1.1 需求 在商品列表页面选中多个商品,然后删除. 1.2 需求分析 此功能要求商品列表页面中的每个商品前有一个checkbox,选中多个商品后点击删除按钮把商品id传给controller,根据商 ...

  2. 开发中经典sql总结

    1.说明:显示文章.提交人和最后回复时间 select a.title,a.username,b.adddate ,(select max(adddate) from table where tabl ...

  3. Linux wget命令

    一.简介 wget是一个Linux系统中的下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS ...

  4. 谈谈我对Ui设计师的一些观点

    做ui设计师3年多了,对ui设计师在工作中也了解了许多. 作为UI设计师,在工作中需要清楚了解设计的目的,尤其是你做的不是大众化产品,不能以个人认知.很强的主题性来确定. 例如针对儿童人群的app时, ...

  5. [SoapUI] Compare JSON Response(比较jsonobject)

    http://jsonassert.skyscreamer.org/ 从这个网站下载jsonassert-1.5.0.jar ,也可以下载到源代码 JSONObject data = getRESTD ...

  6. 【Linux】Memcached安装

    Memcached概念 Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. MemCache的工作流程如下:先检查客户端的请求数据是否在memcached中, ...

  7. 2018.09.10 bzoj1499: [NOI2005]瑰丽华尔兹(单调队列优化dp)

    传送门 单调队列优化dp好题. 这题其实很简单. 我们很容易想到一个O(T∗n∗m)" role="presentation" style="position: ...

  8. CentOS7下安装配置Nginx

    一:安装依赖项 1.pcre:2.openssl:3.zlib:4.gcc:可直接通过yum安装 二:创建nginx账户(可以配置nginx.conf的user为此账户) useradd nginx ...

  9. python编码(七)

    本文中,以'哈'来解释作示例解释所有的问题,“哈”的各种编码如下: 1. UNICODE (UTF8-16),C854:2. UTF-8,E59388:3. GBK,B9FE. 一.python中的s ...

  10. *C语言的小技巧

    计算数组长度 ,,,,}; int Length=sizeof(a)/sizeof(int); 交换a和b的值,不借用辅助变量 a=a+b; b=a-b; a=a-b; 将0-9的字符转化为整数 '; ...