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 ...
随机推荐
- Linux/Windows下安装SonarCube
1. 下载合适的版本,尽量不要下载最新的版本,最新的版本要求Java 11+,如果没有安装最新版的Java的话,尽量用 SonarQube 7.0 以下的版本,SonarQube 7.0是可以用jdk ...
- 小顶堆---非递归C语言来一发
#include <stdio.h> #include <stdlib.h> #define HEAP_SIZE 100 #define HEAP_FULL_VALUE -10 ...
- Route all trafic for specific ip over specific network interface
15 I have a linux server that needs to get some routing. I'm fairly new at this and i don't find any ...
- commons-dbutils使用介绍,commons-dbutils是JDBC的替代品
commons-dbutils是Apache开源组织提供的用于操作数据库的工具包.今天为大家介绍一下该包的常用方法. 对于数据库的操作无外乎增删改查,而增删改本质上可以归为一类,操作方式相同,只是SQ ...
- Backpack IV
Description Given an integer array nums[] which contains n unique positive numbers, num[i] indicate ...
- 使用systemctl管理nginx
[Unit] Description=nginx After=network.target [Service] Type=forking ExecStartPre=/data/apps/nginx/s ...
- javaweb学习笔记(三)
一.javaweb高级(Filter和Listener)的简单介绍 1.过滤器Filter (https://www.cnblogs.com/vanl/p/5742501.html) ①定义 Filt ...
- Linux 文件系统缓存 -针对不同数据库有不同作用
文件系统缓存 filesystem cache 许多人没有意识到.文件系统缓存对于性能的影响.Linux系统默认的设置倾向于把内存尽可能的用于文件cache,所以在一台大内存机器上,往往我们可能发现没 ...
- mage Ansible学习3 ansible role实例
一.ansible配置文件解析 1./etc/ansible/ansible.cfg配置文件详解 [root@node3 ~]# cat /etc/ansible/ansible.cfg |grep ...
- /etc/shells
List of acceptable shells for chpass(1). Ftpd will not allow users to connect who are not using one ...