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 ...
随机推荐
- K8S集群证书已过期且etcd和apiserver已不能正常使用下的恢复方案
在这种比较极端的情况下,要小心翼翼的规划和操作,才不会让集群彻底死翘翘.首先,几个ca根证书是10年期,应该还没有过期.我们可以基于这几个根证书,来重新生成一套可用的各组件认证证书. 前期,先制定以下 ...
- Kotlin编译器优化与when关键字详解
Any类型: 定义一个函数,其参数接受所有类型,对于Java而言Object是所有类的基类,而在Kotlin中得用Any关键字,如下: 其中瞅一下该Any字段是个啥类型: 然后里面做一些判断: 这是因 ...
- java中List集合
List集合是一个元素有序可以重复的集合,集合中每个元素都有其对应的顺序索引.List集合允许使用重复集合,前面博客写到Set不允许有重复集合.List集合可以通过索引来访问指定位置的集合元素. Li ...
- python_并发编程——锁
多进程模拟买票~ import time import json from multiprocessing import Process class Show(Process): #查 def run ...
- http之browser抓包
Chrome 抓包 详解谷歌Network 面板 快捷键:Control+Shift+I (Windows) or Command+Option+I (Mac) 控制器 补充知识: HAR Chrom ...
- python会缓存小的整数和短小的字符
经过测试,python会缓存的小整数的范围是 [-5, 256] # True a = 1 b = 1 print(a is b) # True a = "good" b = &q ...
- Go读写文件
Go序列化和反序列化 package main import ( "bufio" "encoding/json" "fmt" "o ...
- Codeforces Global Round 6[A,B,C]
题意:给一个字符串,对它重新排列使得它是60的倍数. 模拟一下,需要能整除60 字符串中需要 能整除2 3 10,所以需要字符串各位数之和能整除3 并且有 一个偶数和一个0 或者两个0也行[没考虑 ...
- Greenplum 激活standby 和恢复 master 原有角色
当Greenplum segment的primary出现问题时,FTS会监测到,GP会自动激活mirror.但是对于GP的master节点,虽然有standby,但是GP并不会自动来完成master和 ...
- java大文件分块上传断点续传demo
第一点:Java代码实现文件上传 FormFile file = manform.getFile(); String newfileName = null; String newpathname = ...
