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.

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

[LC] 1007. Minimum Domino Rotations For Equal Row的更多相关文章

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

  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. 【LeetCode】1007. Minimum Domino Rotations For Equal Row 解题报告(Python)

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

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

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

  7. LC 712. Minimum ASCII Delete Sum for Two Strings

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  8. LC 983. Minimum Cost For Tickets

    In a country popular for train travel, you have planned some train travelling one year in advance.  ...

  9. LC 539. Minimum Time Difference

    Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...

随机推荐

  1. redis(二)----基本操作

    1. redis介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset( ...

  2. XML--XML Schema Definition(一)

    参考 https://blog.csdn.net/wangw2008/article/details/83195283 https://blog.csdn.net/lmj623565791/artic ...

  3. zabbix监控日志关键字

    1 添加zabbix监控项目 A.选择类型为“”zabbix客户端(主动式)“” B.键值: xx_log.log 为日志的绝对路径 connectException 为关键字 ---需根据自己需要定 ...

  4. h5-多列布局

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Java执行js加密算法

    Java执行js加密算法 今日需求:在后端执行一段加密算法,算法是js写的 明白需求以后疯狂百度.最后发现JDK提供了各种脚本的支持(怪笔者学艺不精,第一次见识到这个库,留下不学无术的泪水),正题开始 ...

  6. 「不会」Min25筛

    大概的思路是把所有数分成质数和合数考虑 对于质数,必须找出一个很简单的完全积性函数和所求函数拟合 把所有数当做质数看待求个前缀和,然后再枚举合数的最小质因子把合数T掉 枚举到根号n,即可保证把n以内的 ...

  7. IDEA抽取方法的快捷键

    正常的话是 ctrl+alt+m 如果快捷键占用或者修改过,在写代码的地方右键->refactor->extract->method

  8. mysql字符集配置&mysql中文乱码

    问题描述 这两天重置了下自己的电脑系统,一个ubuntu,另外一个当然就是windows. 不过在运行程序的时候发现,出现了很多的"????",也就是乱码字符.毫无疑问,这定然是m ...

  9. 104. HttpRequest对象详解

    WSGIRequest 对象常用的属性和方法: WSGIRequest对象常用的属性: WSGIRequest对象大部分属性都是只读的,因为这些属性是从客户端上传上来的,没必要做任何的修改.以下对一些 ...

  10. 基于基因调控网络(Hopfield network)构建沃丁顿表观遗传景观

    基因调控网络的概念在之前已经简要介绍过:https://www.cnblogs.com/pear-linzhu/p/12313951.html 沃丁顿表观遗传景观(The Waddington's e ...