1007. Minimum Domino Rotations For Equal Row
In a row of dominoes,
A[i]
andB[i]
represent the top and bottom halves of thei
-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 thatA[i]
andB[i]
swap values.Return the minimum number of rotations so that all the values in
A
are the same, or all the values inB
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
Approach #1: [Java]
class Solution {
public int minDominoRotations(int[] A, int[] B) {
int n = A.length;
for (int i = 0, a = 0, b = 0; i < n && (A[i] == A[0] || B[i] == A[0]); ++i) {
if (A[i] != A[0]) a++;
if (B[i] != A[0]) b++;
if (i == n-1) return Math.min(a, b);
}
for (int i = 0, a = 0, b = 0; i < n && (A[i] == B[0] || B[i] == B[0]); ++i) {
if (A[i] != B[0]) a++;
if (B[i] != B[0]) b++;
if (i == n-1) return Math.min(a, b);
}
return -1;
}
}
Analysis:
1. try A[0]
2. try B[0]
3. return -1;
one observation is that if A[0] and B[0] are all work, the result will be the same.
Approach #2. HashSet. [Java]
class Solution {
public int minDominoRotations(int[] A, int[] B) {
int n = A.length;
HashSet<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));
int[] countA = new int[7], countB = new int[7];
for (int i = 0; i < n; ++i) {
set.retainAll(new HashSet<>(Arrays.asList(A[i], B[i])));
countA[A[i]]++;
countB[B[i]]++;
}
for (int s : set) return Math.min(n - countA[s], n - countB[s]);
return -1;
}
}
Analysis:
Find intersection s of {A[i], B[i]}.
s.size() == 0: no possible reslut.
s.size() == 1: one and only one reslut.
s.size() == 2: it means all dominoes are [a, b] or [b, a], try either one.
s.size() > 2: impossible.
Reference:
https://www.geeksforgeeks.org/arrays-aslist-method-in-java-with-examples/
https://www.geeksforgeeks.org/set-retainall-method-in-java-with-example/
1007. Minimum Domino Rotations For Equal Row的更多相关文章
- 【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. ( ...
- 【LeetCode】1007. Minimum Domino Rotations For Equal Row 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历一遍 日期 题目地址:https://leetc ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)
Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
随机推荐
- pca总结,非常详细
#coding=utf- from numpy import * '''通过方差的百分比来计算将数据降到多少维是比较合适的, 函数传入的参数是特征值和百分比percentage,返回需要降到的维度数n ...
- C++11新特性——The C++ standard library, 2nd Edition 笔记(一)
前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...
- js把一个数组插入到另一个数组的指定位置
var arr1 = ['a', 'b', 'c']; var arr2 = ['1', '2', '3']; // 把arr2 变成一个适合splice的数组(包含splice前2个参数的数组) a ...
- 2018.10.14 bzoj1915: 奶牛的跳格子游戏(单调队列优化dp)
传送门 NOIP练习题. f[i]f[i]f[i]表示去的时候选了iii且回来的时候第一步走的是i−1i-1i−1的最优值. 显然f[i]=maxf[i]=maxf[i]=max{f[j]−sum[j ...
- 2018.08.22 NOIP模拟 or(线段树)
or [描述] 构造一个长度为 n 的非负整数序列 x,满足 m 个条件,第 i 个条件为x[li] | x[li+1] | - | x[ri]=pi. [输入] 第一行两个整数 n,m.接下来 m ...
- Win7 SP1 提示ADO的问题
需要安装 Windows6.1-KB2640696-v3-x64.msu 这个Pack
- POJ 1458 Common Subsequence (DP+LCS,最长公共子序列)
题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度. 析:很明显是个DP,就是LCS,一点都没变.设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串L ...
- jdk更换不起作用问题
本人前面装了jdk8,现在准备用jdk7,我安装好了jdk7:把系统变量中的JAVA_HOME 改为 D:\java\jdk\jdk7\jdk1.7.0_67,Path 下添加如下变量,记得加;和上一 ...
- struts2从浅至深(六)总结
在我认为strust2的作用就是 1.主要跟前端交互的框架数据提交先经过struts 2.起到对数据的过滤,接受数据 3.把数据显示到前段,具有很成熟的ognl技术,用起来特别方便 4.还提供了跟前段 ...
- HDU1269 迷宫城堡 2016-07-24 13:47 84人阅读 评论(0) 收藏
迷宫城堡 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的 ...