LeetCode 1007. 行相等的最少多米诺旋转 题解

示例

复制代码
输入:tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
输出:2
解释: 
图一表示:在我们旋转之前, tops 和 bottoms 给出的多米诺牌。 
如果我们旋转第二个和第四个多米诺骨牌,我们可以使上面一行中的每个值都等于 2

对于本题,我的做题思路是先观察后面分支思考,先说说我的观察,通过题目的示例观察,只有当一个数字的总数等于或超过tops.length后才有可能做到交换出一个所有值都同为一个数的数组,通过该规律,我们可以找到我们需要统计的数值mid,然后通过条件判断,如果两个数组中遍历到相同下标时都没出现mid那就直接return false,但我们最后需要的结果是最少交换几次即可,那我们在遍历的时候便可以统计出来,但是当tops和bottoms数组中同时出现mid时就不必要交换了,直接在后面减掉即可。

复制代码
class Solution {
    public int minDominoRotations(int[] tops, int[] bottoms) {
        HashMap<Integer,Integer> maptop = new HashMap<>();
        HashMap<Integer,Integer> mapbo = new HashMap<>();

        int n = tops.length;

        int num = 0;
        int mid = 0;
        for(int i=0;i<n;i++){
            maptop.put(tops[i],maptop.getOrDefault(tops[i],0)+1);
            mapbo.put(bottoms[i],mapbo.getOrDefault(bottoms[i],0)+1);
            if(maptop.getOrDefault(tops[i],0)>=(n+1)/2) mid = tops[i];
            if(mapbo.getOrDefault(bottoms[i],0)>=(n+1)/2) mid = bottoms[i];
        }
        if(maptop.getOrDefault(mid,0)+mapbo.getOrDefault(mid,0) < n) return -1;
        for(int i=0;i<n;i++){
            if(tops[i]!=mid&&bottoms[i]!=mid) return -1; 
            if(tops[i]==mid&&bottoms[i]==mid) num++;
        }
        return Math.min(maptop.get(mid)-num,mapbo.get(mid)-num);
    }
}

画个图来更好的说明我的思路

说的可能还是不太详细,因为有一些得规避一下,所以代码最后成型如此,比如有长度为7的数组,

那么(int)7/2 = 3,3+3=6<7不可以成立一个值相同的数组,所以要(int)(7+1)/2=4才行,这就是我们需要的mid值,然后后面一遍遍历找出同一个下标下的所有情况。

我们来看下题解又是怎么解决的

复制代码
class Solution {
    public int minDominoRotations(int[] tops, int[] bottoms) {
        int ans = Math.min(minRot(tops, bottoms, tops[0]), minRot(tops, bottoms, bottoms[0]));
        return ans == Integer.MAX_VALUE ? -1 : ans;
    }

    private int minRot(int[] tops, int[] bottoms, int target) {
        int toTop = 0;
        int toBottom = 0;
        for (int i = 0; i < tops.length; i++) {
            int x = tops[i];
            int y = bottoms[i];
            if (x != target && y != target) {
                return Integer.MAX_VALUE;
            }
            if (x != target) {
                toTop++; // 把 y 旋转到上半
            } else if (y != target) {
                toBottom++; // 把 x 旋转到下半
            }
        }
        return Math.min(toTop, toBottom);
    }
}

以上是灵神的代码,看了下来忽然让我意识到了一点,就是,如果需要一个数组的值全变为同一个数的话,那就一定会是tops[0]或者bottoms[0]

然后后面的步骤都会和我差不多,确定了target后就开始遍历找到最小的反转次数。说是脑筋急转弯也不为过。

相关推荐
地平线开发者10 小时前
J6B vio scenario sample
算法
BothSavage1 天前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn1 天前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
烬羽1 天前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
先吃饱再说2 天前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰2 天前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术2 天前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六2 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程