题目如下:

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. 1 <= A[i], B[i] <= 6
  2. 2 <= A.length == B.length <= 20000

解题思路:因为 1 <= A[i], B[i] <= 6,所以如果能使得A或者B中所有元素的值一样,那么就只有12种情况,即A中元素或者B中元素全为1/2/3/4/5/6,依次判断这6种情况即可,如假设变换后A中元素全为1,从头遍历A与B,如果A[i] != 1 并且B[i] != 1表示无法使得A中元素全为1,继续判断2的情况;否则如果A[i] != 1 并且B[i] = 1,那么交换的次数加1;同理可求得B中元素也全为1的交换次数。遍历完这6种情况后,如果无法满足则返回-1,可以的话返回交换的最小值。

代码如下:

class Solution(object):def minDominoRotations(self, A, B):""":type A: List[int]:type B: List[int]:rtype: int"""res = 20001for i in range(1,7):a_move = 0b_move = 0a_flag = Trueb_flag = Truefor j in range(len(A)):if A[j] != i:if B[j] != i:a_flag = Falseelse:a_move += 1if B[j] != i:if A[j] != i:b_flag = Falseelse:b_move += 1if a_flag == False and b_flag == False:breakif a_flag:res = min(res,a_move)if b_flag:res = min(res,b_move)return res if res != 20001 else -1

转载于:https://www.cnblogs.com/seyjs/p/10508861.html

【leetcode】1007. Minimum Domino Rotations For Equal Row相关推荐

  1. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  2. 【leetcode】931. Minimum Falling Path Sum

    题目如下: Given a square array of integers A, we want the minimum sum of a falling path through A. A fal ...

  3. 【Leetcode】1526. Minimum Number of Increments on Subarrays to Form a Target Array(配数学证明)

    题目地址: https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array ...

  4. 【Leetcode】1335. Minimum Difficulty of a Job Schedule

    题目地址: https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/ 给定一个长nnn正整数数组AAA和一个正整数ddd, ...

  5. 【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)

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

  6. 【Leetcode】1824. Minimum Sideway Jumps

    题目地址: https://leetcode.com/problems/minimum-sideway-jumps/description/ 给定一条宽为333的路,可以视为三条平行线,给定一个长n+ ...

  7. 【Leetcode】871. Minimum Number of Refueling Stops

    题目地址: https://leetcode.com/problems/minimum-number-of-refueling-stops/description/ 有一个小车,初始位置在 0 0 0 ...

  8. python棋盘最短路径_【leetcode】64. Minimum Path Sum 棋盘最短路径

    1. 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...

  9. 【LeetCode】982. Triples with Bitwise AND Equal To Zero 解题报告(C++)

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

最新文章

  1. asp.net序列化
  2. Redis的安装以及基本操作简介
  3. 美团Android自动化之旅—生成渠道包
  4. matlab sizeof size,使用Matlab进行MEXing C时,size_t和mwSize之间的差异
  5. 牛逼!9种不同的方法帮助你提高国内访问Github的速度到2MB/s以上...
  6. 项目管理能力提升四要素
  7. 独家 | 手把手教随机森林
  8. 蔡司数码视疲劳测试软件_[专题]重度近视别担心!!蔡司数码型镜片真实测评,带给我不一样的体验...
  9. Excel数据分类汇总与数据透视表
  10. VB中数组的嵌套循环
  11. 打造高效研发团队 (1) —— 组织架构篇
  12. 冯提出的计算机工作原理是,冯诺依曼提出的计算机的基本工作原理是什么
  13. golang数据结构初探之管道chan
  14. 计算机应用基础心得体会300字,网络远程学习的心得体会
  15. 地域和地方的区别_经济区域和区域经济的区别和联系是什么
  16. udp 与 tcp 通信编程
  17. SDK对于APP主流量变现意味着什么?
  18. 嵌入式系统与通用计算机操作系统的区别
  19. Matlab实验(一)
  20. 如何用Eclipse编写第一个Java应用程序

热门文章

  1. 08-cmake语法-set()
  2. oracle 12c创建可插拔数据库(PDB)与用户详解
  3. You don't have permission to access
  4. 添加sudo权限脚本
  5. xamarin 学习笔记02- IOS Simulator for windows 安装
  6. Android OpenGL ES 离屏渲染(offscreen render)
  7. 深入cocos2d-x中的touch事件
  8. javascript跨域实践
  9. 怎么将算法改成程序_多肉烂根怎么办?将土培改成水培,长势好,叶子变得更水灵...
  10. 【Groovy】Groovy 方法调用 ( 使用 对象名.@成员名 访问 Groovy 对象成员 )