题目如下:

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 = 20001
for i in range(1,7):
a_move = 0
b_move = 0
a_flag = True
b_flag = True
for j in range(len(A)):
if A[j] != i:
if B[j] != i:
a_flag = False
else:
a_move += 1
if B[j] != i:
if A[j] != i:
b_flag = False
else:
b_move += 1
if a_flag == False and b_flag == False:
break
if a_flag:
res = min(res,a_move)
if b_flag:
res = min(res,b_move)
return res if res != 20001 else -1

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

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

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

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

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

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

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

  7. 【leetcode】963. Minimum Area Rectangle II

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

  8. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  9. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

随机推荐

  1. mybatis获取数据库自增id

    http://blog.csdn.net/dyllove98/article/details/8866357 http://www.iteye.com/problems/86864 insert标签中 ...

  2. 如何查看MySQL数据库的版本

    如何查看MySQL数据库的版本 一.总结 一句话总结: SQL语句:select version(); 命令行:mysql -V 或 mysql --version 二.三种方法查看MySQL数据库的 ...

  3. Integer自动装箱和拆箱

    Integer a=3;   =>    Integer a=Integer.valueOf(3); /** *@description: 自动装箱和拆箱 *@auther: yangsj *@ ...

  4. Gogs 安装 - 本地安装,容器安装

    文章目录 安装 Gogs 本地安装 前提条件: 数据库 git 创建 git 用户 SSH 服务器 安装 升级 配置及运行 配置 运行 Gogs 服务 在线安装 Gogs 后台运行 gogs 通过 d ...

  5. python实现操作mysql数据库

    实现代码如下: #mysql数据库的查询等 import pymysql from xctest_tools.xc_ReadFile.get_ReadTxt import * class mysql: ...

  6. SmokeTest测试流程

    没办法了,本来是表格,但是粘贴不过来 测试目的: 用于检测该版本在基本的应用场景下,基本的功能是否满足. 测试前提: 发货版本 示例:ATV9冒烟测试测试项解读 表格获取:Google ATV hel ...

  7. 【Unity练习】 平衡球Demo

    链接:http://pan.baidu.com/s/1pKEpnIz 密码:btke

  8. HDU 6583 Typewriter 题解

    ——本题来自杭电多校第一场 题意:给定一个字符串,主角需要用打字机将字符串打出来,每次可以: 1.花费p来打出任意一个字符 2.花费q来将已经打出的某一段(子串)复制到后面去 对于这种最优化的问题,我 ...

  9. Spring MVC浅析

    讲到MVC,想必大家都很熟悉,就是将数据模型.视图.控制器进行分离,做到分工明确,在Spring的帮助下,Spring MVC 更是做到了充分的解耦,因为大部分的资源都由Spring进行管理,为Spr ...

  10. Learning OSG programing---osgShape

    本例示范了osg中Shape ---- 基本几何元素的绘制过程.参照osg官方文档,Shape 类包含以下子类: 在示例程序中,函数createShapes函数用于生成需要绘制的几何形状. osg:: ...