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:

Input: A = [2,1,2,4,2,2], B = [5,2,6,2,3,2]
Output: 2
Explanation:
The first figure represents the dominoes as given by A and B: before we do any rotations.
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: Input: A = [3,5,1,2,3], B = [3,6,3,3,4]
Output: -1
Explanation:
In this case, it is not possible to rotate the dominoes to make one row of values equal. Note: 1 <= A[i], B[i] <= 6
2 <= A.length == B.length <= 20000

1. The final uniform character should be either A[0] or B[0]

2. A[0] could be at the top, or the bottom. Same applies to B[0]

3. If A[0] works, no need to check B[0]; Because if both A[0] and B[0] exist in all dominoes, the result should be the same.

 class Solution {
public int minDominoRotations(int[] A, int[] B) {
if (A.length < 1 || B.length < 1 || A.length != B.length) return -1;
int n = A.length;
for (int i = 0, a = 0, b = 0; i < n && (A[0] == A[i] || A[0] == B[i]); i ++) {
if (A[i] != A[0]) a ++; // a stands for try to put A[0] at top
if (B[i] != A[0]) b ++; // b stands for try to put A[0] at bottom
if (i == n - 1) return Math.min(a, b);
} for (int i = 0, a = 0, b = 0; i < n && (B[0] == A[i] || B[0] == B[i]); i ++) {
if (A[i] != B[0]) a ++;
if (B[i] != B[0]) b ++;
if (i == n - 1) return Math.min(a, b);
}
return -1;
}
}

Leetcode: Minimum Domino Rotations For Equal Row的更多相关文章

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

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

  2. 【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.  ( ...

  3. [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 ...

  4. 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 ...

  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. [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 ...

  7. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  8. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

随机推荐

  1. 微信小程序~获取位置信息

    微信小程序提供的getlocation来获取用户的定位,能够得到用户的经纬度信息 (注:getloaction需要用户授权scope.userLocation)结合map组件能够得到用户的详细定位 & ...

  2. 《The One!团队》第八次作业:ALPHA冲刺(二)

    项目 内容 作业所属课程 所属课程 作业要求 作业要求 团队名称 < The One !> 作业学习目标 (1)掌握软件测试基础技术.(2)学习迭代式增量软件开发过程(Scrum) 第二天 ...

  3. destoon标签大集合

    最近没事玩上了destoon,所以就自己花一点时间整理了一下destoon标签,对开发有帮助,本篇文章由博客园-圆柱模板  博主整理发布 1.全局标签 网站名称:{$DT[sitename]} 网站地 ...

  4. spring+mybatis通用dao层、service层的实现

    个人理解: 1.mybatis-spring.jar 提供了SqlSessionTemplate类该类可以对数据库进行CRUD操作(底层其实还是SqlSession) 2.我们可以集成SqlSessi ...

  5. 业需软需word小技巧

    首先要看看word格式模板设计规则 一.页面材料格式模板  1. 页边距:上下边距为2.54cm:左右边距为2.8cm 2. 页眉.页脚:页眉为1.5cm:页脚为1.75cm 3. 行间距:20p行距 ...

  6. sqlite3中给表添加列

    1.修改表名为临时表 ALTER TABLE {tableName} RENAME TO TempOldTable; 2.创建新表,跟原来的表名一致 CREATE TABLE {tableName} ...

  7. IDEA 中tomcat图片储存和访问虚拟路径(图片和程序分家)

    本文链接:https://blog.csdn.net/qq_36481052/article/details/78813213 **前段时间,遇到了图片已经储存了文件中也显示有图片,但就是死活访问不到 ...

  8. go内置的反向代理

    package main import ( "log" "net/http" "net/http/httputil" "net/u ...

  9. php实现大文件上传分片上传断点续传

    前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...

  10. shell编程题(一)

    求2个数之和 #!/bin/bash function add { )); then echo "The arg in't correct" else +$)) echo $sum ...