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 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的更多相关文章
- 【LeetCode】1007. Minimum Domino Rotations For Equal Row 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历一遍 日期 题目地址:https://leetc ...
- 【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. ( ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [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 ...
随机推荐
- pyspark AttributeError: 'NoneType' object has no attribute 'setCallSite'
pyspark: AttributeError: 'NoneType' object has no attribute 'setCallSite' 我草,是pyspark的bug.解决方法: prin ...
- nginx简单学习
1.nginx的安装 .yum解决编译nginx所需的依赖包,之后你的nginx就不会报错了 yum install gcc patch libffi-devel python-devel zlib- ...
- 行为型模式(四) 观察者模式(Observer)
一.动机(Motivate) "观察者模式"在现实生活中,实例其实是很多的,比如:八九十年代我们订阅的报纸,我们会定期收到报纸,因为我们订阅了.银行可以给储户发手机短信,也是&qu ...
- 域渗透:LSA Protection
简介:微软在 2014 年 3 月 12 日添加了 LSA 保护策略,用来防止对进程 lsass.exe 的代码注入,这样一来就无法使用 mimikatz 对 lsass.exe 进行注入,相关操作也 ...
- b、B、KB、MB、GB 的关系?
1. 8bit (位) = 1Byte (字节) 2.1024Byte (字节 ) = 1KB 3.1024KB = 1MB 4.1024MB = 1GB 5.1024GB = 1TB
- 讲心情 demo1
讲道理找了一个安心而又稳定的工作. 每天活闲的蛋疼, 这种对于老年人来说可能会很好,但是,对于一个24岁的人可能就是坟墓了. 么事呻吟一下. 爬虫这条路越来越远了. 写下今年计划吧..机器学习入下 ...
- Linux 服务器用户间ssh免密码登录
1.本脚本为服务器用户间密码互信登录脚本 2.依赖 需要安装expect工具 3.使用 sh CreateUserssh.sh hadoop hadoop /home/hadoop/.ssh 三个带入 ...
- 转载:Spark GraphX详解
1.GraphX介绍 1.1 GraphX应用背景 Spark GraphX是一个分布式图处理框架,它是基于Spark平台提供对图计算和图挖掘简洁易用的而丰富的接口,极大的方便了对分布式图处理的需求. ...
- Python 程序打包成 exe 可执行文件
Python 程序打包工具 Python 是一个脚本语言,被解释器解释执行.它的发布方式: .py 文件:对于开源项目或者源码没那么重要的,直接提供源码,需要使用者自行安装 Python 并且安装依赖 ...
- MySQL UPDATE 更新
以下是 UPDATE 命令修改 MySQL 数据表数据的通用 SQL 语法: UPDATE table_name SET field1=new-value1, field2=new-value2 [W ...
