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.

思路:就是只会有一个数 count >= n 否则,不可能满足条件;count >= n 可以用hashmap做,也可以用一个数学表达式来表示,那么就是每个位置上必须有个i,那么countA(i) + countB(i) - same(i) >= n;

方法1:用hashmap count

class Solution {
    public int minDominoRotations(int[] A, int[] B) {
        if(A == null || B == null || A.length == 0 || B.length == 0 || A.length != B.length) {
            return -1;
        }
        
        HashMap<Integer, Integer> hashmap = new HashMap<Integer, Integer>();
        count(hashmap, A);
        count(hashmap, B);
        int n = A.length;
        int num = -1;
        for(Integer key: hashmap.keySet()) {
            if(hashmap.get(key) >= n) {
                num = key;
            }
        }
        // cannot find count(num) >= n;
        if(num == -1) {
            return -1;
        }
        
        int flipA = flip(A, B, num);
        int flipB = flip(B, A, num);
        if(flipA == -1 && flipB != -1) {
            return flipB;
        } else if(flipA != -1 && flipB == -1) {
            return flipA;
        } else {
            return Math.min(flipA, flipB);
        }
    }
    
    private int flip(int[] A, int[] B, int num) {
        int count = 0;
        for(int i = 0; i < A.length; i++) {
            if(A[i] != num) {
                if(B[i] == num) {
                    count++;
                } else {
                    return -1;
                }
            }
        }
        return count;
    }
    
    private void count(HashMap<Integer, Integer> hashmap, int[] A) {
        for(int i = 0; i < A.length; i++) {
            if(hashmap.containsKey(A[i])) {
                hashmap.put(A[i], hashmap.get(A[i]) + 1);
            } else {
                hashmap.put(A[i], 1);
            }
        }
    }
}

方法2: 用countA(i) + countB(i) - same(i) >= n 来表示;

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值