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.

Idea 1. Bruteforce, swap or not swap(0-1), similar to subsets problem, typical backtracking

Time complexity: O(n2^n)

Space complexity: O(1)

 class Solution {
private void swap(int[] A, int[] B, int pos) {
int temp = A[pos];
A[pos] = B[pos];
B[pos] = temp;
}
private boolean isEqual(int[] A) {
for(int i = 1; i < A.length; ++i) {
if(A[i] != A[i-1]) {
return false;
}
}
return true;
} private void helper(int[] A, int[] B, int pos, int currCnt, int[] cnt) {
if(pos == A.length) {
if(isEqual(A) || isEqual(B)) {
cnt[0] = Math.min(cnt[0], currCnt);
}
return;
} if(A[pos] != B[pos]) {
swap(A, B, pos);
helper(A, B, pos+1, currCnt+1, cnt);
swap(A, B, pos);
} helper(A, B, pos+1, currCnt, cnt);
}
public int minDominoRotations(int[] A, int[] B) {
int[] cnt = new int[1];
cnt[0] = Integer.MAX_VALUE;
helper(A, B, 0, 0, cnt);
return cnt[0] == Integer.MAX_VALUE? -1: cnt[0];
}
}

Idea 2. 有时候具体的题目要求更restrict, 反而简化了问题,这题要求all elments equal in A[i] or B[i], 如果我们知道交换后的结果数组的相同数,只能是四种:A-> { A[0], B[0] }, B-> { A[0], B[0] },

make A be all A[0] or B[0]

make B be all A[0] or B[0]

然后计算最小步数

Time complexity: O(n), 4 times scan

Space complexity: O(1)

 class Solution {
int helper(int[] A, int[] B, int target) {
int cnt = 0;
for(int i = 0; i < A.length; ++i) {
if(A[i] != target) {
if(B[i] == target) {
++cnt;
}
else {
return Integer.MAX_VALUE;
}
}
}
return cnt;
}
public int minDominoRotations(int[] A, int[] B) {
int result = Math.min(helper(A, B, A[0]),
helper(A, B, B[0]));
result = Math.min(result,
Math.min(helper(B,A, B[0]),
helper(B, A, A[0])));
return result == Integer.MAX_VALUE? -1: result;
}
}

Idea 2.a 网上看到的,一次遍历同时计算A,B所需的步数

Time complexity: O(n), 2 times scan

Space comlexity: O(1)

 class Solution {
private int helper(int[] A, int[] B, int target) {
int swapA = 0, swapB = 0;
for(int i = 0; i < A.length; ++i) {
if(A[i] != target && B[i] != target) {
return Integer.MAX_VALUE;
} if(A[i] != target){
++swapA;
}
else if(B[i] != target) {
++swapB;
}
} return Math.min(swapA, swapB);
}
public int minDominoRotations(int[] A, int[] B) {
int result = Math.min(helper(A, B, A[0]),
helper(A, B, B[0])); return result == Integer.MAX_VALUE? -1: result;
}
}

Idea 3. intersection set of {A{i}, B{i}}, 为了完成swap可以让数组相等,each position in either A or B should have the element, we can use set.retailAll, the steps = A.length - countA[A[i]]

Time complexity: O(n)

Space complexity: O(1), HashMap + HashSet

 class Solution {
public int minDominoRotations(int[] A, int[] B) {
Set<Integer> candidates = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));
int[] countA = new int[7];
int[] countB = new int[7]; for(int i = 0; i < A.length; ++i) {
++countA[A[i]];
++countB[B[i]];
candidates.retainAll(new HashSet<>(Arrays.asList(A[i], B[i])));
} for(int val: candidates) {
return Math.min(A.length - countA[val], A.length - countB[val]);
} return -1;
}
}

用数组代表set

 class Solution {
public int minDominoRotations(int[] A, int[] B) {
int[] countA = new int[7];
int[] countB = new int[7];
int[] common = new int[7]; for(int i = 0; i < A.length; ++i) {
++countA[A[i]];
++countB[B[i]];
if(A[i] == B[i]) {
++common[A[i]];
}
} for(int i = 1; i < 7; ++i) {
if(countA[i] + countB[i] - common[i] >= A.length) {
return Math.min(A.length - countA[i], A.length - countB[i]);
}
} return -1;
}
}

Minimum Domino Rotations For Equal Row LT1007的更多相关文章

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

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

  4. 【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.  ( ...

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

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

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

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)

    Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...

  9. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

随机推荐

  1. Scrapy实战篇(三)之爬取豆瓣电影短评

    今天的主要内容是爬取豆瓣电影短评,看一下网友是怎么评价最近的电影的,方便我们以后的分析,以以下三部电影:二十二,战狼,三生三世十里桃花为例. 由于豆瓣短评网页比较简单,且不存在动态加载的内容,我们下面 ...

  2. MySQL数据库的库表基本操作

    一.库操作 1.创建业务数据库 DDL 数据库命名规则:区分大小写.唯一性.不能使用关键字如 create select.不能单独使用数字 语法:CREATE DATABASE 数据库名; CREAT ...

  3. IIS小知识

    1.IIS的默认配置路径 IIS5.0默认配置文件路径C:\WINNT\system32\inetsrv\MetaBase.binIIS6.0默认配置文件路径C:\WINDOWS\system32\i ...

  4. redis集群相关

    1.主从数据库配置 为master数据库添加slave数据库只需要在从数据库的配置中添加配置: slaveof 主数据库地址 主数据库端口 当然,也可以通过命令: redis-server --por ...

  5. i++ 是线程安全的吗?

    相信很多中高级的 Java 面试者都遇到过这个问题,很多对这个不是很清楚的肯定是一脸蒙逼.内心肯定还在质疑,i++ 居然还有线程安全问题?只能说自己了解的不够多,自己的水平有限. 先来看下面的示例来验 ...

  6. IDEA VM设置

    1.IDEA vm options -server -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128 ...

  7. iphone 开发h5 踩过的坑

    html,body{ -webkit-text-size-adjust: none; }  // 当需要在中文版chrome浏览器中显示小于12px的字体时,而且此时页面放大效果会被阻止 html,b ...

  8. 一分钟搭建Spring Boot

    1.首先你的电脑需要安装jdk.Apache Maven.Intellij IDEA 2.新建项目  (敲重点,有的同学有没有Spring Initializr 这个请到本文章后面看安装步骤) 3.选 ...

  9. zabbix安装脚本

    #!/bin/bash # #安装zabbix源.aliyun YUM源 #curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyu ...

  10. git从远程分支clone项目到本地,切换分支命令,其他常用命令

    1.在git命令窗口输入git clone git@139.129.217.217:sg/sgsq_cms.git 回车,即可克隆远程项目到本地.红色字体为远程分支的SSHkey,可以登录到gitli ...