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的更多相关文章

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

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

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

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

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

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

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

  8. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

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

随机推荐

  1. 使用git clone 出现 Permission denied 解决办法

    从git复制项目到本地的一种方式是使用ssh方式,即在git bash中运行命令:git clone git@github.com:***.git 此种方式下载代码到本地的时候,可能出现Permiss ...

  2. 使用vue打包,vendor文件过大,或者是app.js文件很大

    我的解决办法: 1.把不常改变的库放到index.html中,通过cdn引入,比如下面这样: 然后找到build/webpack.base.conf.js文件,在 module.exports = { ...

  3. Mybatis控制台打印SQL语句的两种方式

    问题描述在使用mybatis进行开发的时候,由于可以动态拼接sql,这样大大方便了我们.但是也有一定的问题,当我们动态sql拼接的块很多的时候,我们要想从*mapper.xml中直接找出完整的sql就 ...

  4. [python]赶集网二手房爬虫插件【可用任意扩展】

    最近应一个老铁的要求,人家是搞房产的,所以就写了这个二手房的爬虫,因为初版,所以比较简单,有能力的老铁可用进行扩展. import requests import os   from bs4 impo ...

  5. Java中使用BufferedReader的readLine()方法和read()方法来读取文件内容

    目标:读文件 编程时,有很多时候需要读取本地文件,下面介绍一下读取方式: 读单行文件 package com; import java.io.*; import java.util.ArrayList ...

  6. am335x system upgrade kernel gpio(九)

    1      Hardware Overview gpio interface,pin map: AM335X_I2C0_W_C----------------------MCASP0_AXR1 /* ...

  7. php技能树---大神的进阶之路

    PHP7 迎来巨大的性能提升,又一次回到关注的焦点.根据这些年在开发圈子总结的LNMP程序猿发展轨迹,结合个人经验体会,总结出很多程序员对未来的迷漫,特别对技术学习的盲目和慌乱,简单梳理了这个每个阶段 ...

  8. redis系列(四):切换RDB备份到AOF备份

    1.准备环境 redis.conf服务端配置如下: daemonize yes port logfile /data//redis.log dir /data/ dbfilename dbmp.rdb ...

  9. 一个简单的setTimeout的问题

    经常使用 setTimeout,并且自己认为也算比较熟悉这个函数的用法. 不过今天碰到了,突然发现自己没有意识到这个问题.于是作为一个记录点,进行巩固一下. var timer = window.se ...

  10. SQL数据清洗

    大家好,我是jacky,很高兴继续跟大家分享<MySQL数据分析实战>,从本节课程开始,我们的课程就会变得越来越实战,也会越来越有意思了: 我们课程的主体叫MySQL数据分析实战,那我们用 ...