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. SpringMVC_执行原理

    什么是SpringMVC 概述 Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架. 查看官方文档:https://docs.spring.io ...

  2. POJ 3438:Look and Say

    Look and Say Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9196   Accepted: 5566 Desc ...

  3. 移动端H5开发遇到的问题及解决方法

    本篇文章给大家带来的内容是关于移动端H5开发遇到的问题及解决方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 微信分享签名错误invalid signature vue单页应用hi ...

  4. MySQL-TPS,QPS到底是什么

    计算TPS,QPS的方式 qps,tps是衡量数据库性能的关键指标,网上普遍有两种计算方式 TPS,QPS相关概念 QPS:Queries Per Second         查询量/秒,是一台服务 ...

  5. php封装一个接口类

    <?phpClass Response{//返回数据 public static function show($code,$message='',$data='',$type = 'json', ...

  6. (递归)P1036 选数

    #include<stdio.h>#include<math.h>int x[20],n,k,i; //判断是否质数 int isprime(int n){    for(i= ...

  7. Mybatis之二级缓存(八)

    1. 介绍 Mybatis缓存分为一级缓存和二级缓存,在本节中我们介绍下二级缓存的使用及其特性 MyBatis的一级缓存是在一个Session域内有效的,当Session关闭后,缓存内容也随之销毁.但 ...

  8. OpenMP笔记(二)

    原文:https://www.bearoom.xyz/2019/02/18/openmp2/ OpenMP是由三部分组成的:指令.库函数和环境变量. 一.指令 在C/C++中使用OpenMP需要用到的 ...

  9. c# 多线程——入门学习

    1. 概念介绍 1.1 线程 线程是操作系统能够进行运算调度的最小单位,包含在进程之中,是进程中的实际运作单位.一条线程指的时进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不 ...

  10. 计算机ASCII码对照表

    ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 0 NUT 32 (space) 64 @ 96 . 1 SOH 33 ! 65 A 97 a 2 ST ...