作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/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 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

题目大意

有一排多米诺骨牌,A, B分别代表每个牌的正反面。现在要求我们翻转其中的一部分骨牌,使得正反面对调,能不能让A或者B出现完全相同的点数?如果可以的话,需要返回最少的翻转次数。如果不可以,需要返回-1.

解题方法

遍历一遍

我们需要对这个题做分析:先把A,B两面的数字做一个统计,如果出现最多的点数<牌的个数,那么无论如何都无法翻转成功。如果出现最多的点数=牌的个数,那么这个时候需要保证每一个牌的都有且只有一面的点数是这个最多的点数。如果出现最多的点数>牌的个数,那么需要保证每个牌都至少有一面是这个数字,此时两面都是这个数字的话,就不用翻转。

所以:

如果两面相等都等于出现最多的数字target,不用翻转。

我们需要使用两个数字分别表示向该面翻转的次数。如果只有一面等于target,就把向另一面翻转的次数+1.

在任何时候,只要这个牌的正反面有一面不是出现最多的数字,那么一定返回-1.

我们最终只需要返回两个方向翻转的次数的最小值即可。

下面做一下讨论:为什么只用统计出现最多的数字就行,为什么出现次数第二多的次数一定没有机会?

1.如果出现最多的数字的次数<长度,无论第一第二都不行。
2.如果出现最多的次数=长度,这个时候第二多次数如果等于N,那么两者效果一样,如果第二多次数如果小于N,那么不可能。
3.如果出现最多的次数>N,这个时候一定会出现某些牌的正反面都是该最多的数字。此时第二多的数字没有机会。

总之,只需要判断出现最多的数字即可。

Python代码如下:

class Solution(object):
def minDominoRotations(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
N = len(A)
count = collections.Counter(A + B)
if count.most_common(1)[0][1] < N:
return -1
target = count.most_common(1)[0][0]
a_swap = 0
b_swap = 0
for i in range(N):
if A[i] == B[i]:
if A[i] == target:
continue
else:
return -1
elif A[i] == target:
b_swap += 1
elif B[i] == target:
a_swap += 1
else:
return -1
return min(a_swap, b_swap)

日期

2019 年 3 月 10 日 —— 周赛进了第一页!

【LeetCode】1007. Minimum Domino Rotations For Equal Row 解题报告(Python)的更多相关文章

  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. 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】548. Split Array with Equal Sum 解题报告(C++)

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

  8. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  9. 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)

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

随机推荐

  1. 简单mvc框架核心笔记

    简单mvc框架核心笔记 看了thinkphp5的源码,模仿写了一个简单的框架,有一些心得笔记,记录一下 1.目录结构 比较简单,没有tp那么复杂,只是把需要的核心类写了一些. 核心类库放在mykj里, ...

  2. html5的canvas鼠标点击画圆

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  3. Shell 输出第五行的内容

    目录 Shell 输出第五行的内容 题目 题解-awk 题解-sed Shell 输出第五行的内容 题目 写一个 bash脚本以输出一个文本文件 nowcoder.txt 中第5行的内容. 示例: 假 ...

  4. day11 函数

    day11 函数 一.函数基础 """ 1 什么是函数 函数是盛放代码的容器:把实现某一功能的代码放到一个函数内就制造一个工具 2 为何要用函数 没有用函数之前程序的问题 ...

  5. Spark(八)【利用广播小表实现join避免Shuffle】

    目录 使用场景 核心思路 代码演示 正常join 正常left join 广播:join 广播:left join 不适用场景 使用场景 大表join小表 只能广播小表 普通的join是会走shuff ...

  6. org.apache.hadoop.hive.ql.metadata.HiveException: Internal Error: cannot generate all output rows for a Partition解决

    自己在路径访问明细表开发时,写的sql如下 SELECT guid, sessionid, event['url'] as page, `timestamp` as ts, row_number() ...

  7. 【leetcode】222. Count Complete Tree Nodes(完全二叉树)

    Given the root of a complete binary tree, return the number of the nodes in the tree. According to W ...

  8. linux添加用户、权限

    # useradd –d /usr/sam -m sam 此命令创建了一个用户sam,其中-d和-m选项用来为登录名sam产生一个主目录/usr/sam(/usr为默认的用户主目录所在的父目录). 假 ...

  9. oracle 以SYSDBA远程连接数据库

    在服务器用sysdba登陆 grant sysdba to system 然后在远程就可以sysdba登陆数据库了

  10. Linux服务器---drupal

    Drupal Drupal为用户提供各种工具来管理网站,它可以帮助用户入门,建立自己的网站 1.下载drupal软件(https://www.drupal.org/project/drupal/rel ...