leetcode 1007. Minimum Domino Rotations For Equal Row(行相等的最少多米诺旋转)

某物data魔改与变种算法还原(含加解密Py纯算) 某物data加解密、设备指纹魔改、变异、自定义加密分析与纯算还原 阅读详情

In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)

We may rotate the ith domino, so that tops[i] and bottoms[i] swap values.

Return the minimum number of rotations so that all the values in tops are the same, or all the values in bottoms are the same.

If it cannot be done, return -1.

Example 1:
Input: tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
Output: 2
Explanation:
The first figure represents the dominoes as given by tops and bottoms: 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: tops = [3,5,1,2,3], bottoms = [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.

Constraints:

2 <= tops.length <= 2 * 104
bottoms.length == tops.length
1 <= tops[i], bottoms[i] <= 6

有上下两行骰子的点数,交换点数使上或下行的点数都一样,返回最少的交换次数,无法得到一行一样时返回-1。
上下行点数可以是一样的。

思路:
满足交换后整行点数一样的前提是,该点数出现在上下任一行的次数==tops的长度。
其中,上下两行都出现该点数时算一次。

因为点数只有1~6,可用一个数组保存每个点数出现过的次数(上下行都出现算一次)。
然后遍历这些次数,次数==tops的长度时认为可以达到条件。

最后要计算最小交换次数,
当上下两行出现一样的点数时不需要交换,那只需要看上下两行不一样的点数就行了,
比如
在这里插入图片描述
去掉最后一列点数一样的,点数2在上一行出现了3次,在下一行出现了2次。
显而易见最小交换次数是2次,也就是min(3, 2)。

用一个数组top存入每个点数在上一行出现的次数(上下行一样的不计入),
数组down存入每个点数在下一行出现的次数(上下行一样的不计入),它们中较小的就是需要交换的次数。

    public int minDominoRotations(int[] tops, int[] bottoms) {
        int[] cnt = new int[7];
        int[] top = new int[7];
        int[] down = new int[7];
        int result = 20000;
        int n = tops.length;
        
        for(int i = 0; i < n; i ++) {
            int id1 = tops[i];
            int id2 = bottoms[i];
            cnt[id1] ++;
            if(id1 != id2) {
                cnt[id2] ++;
                top[id1] ++;
                down[id2] ++;
            }
            
        }
        
        for(int i = 1; i <= 6; i ++) {
            if(cnt[i] == n) {
                int min = Math.min(top[i], down[i]);
                result = Math.min(result, min);
            }
        }
        return result == 20000 ? -1 : result;
    }
2026年运维工程师常见面试选择题 这篇文章总结了30道Linux运维面试常见问题及解析,涵盖基础命令、系统监控、故障排查、数据库、缓存、容器编排等多个方面。主要内容包括: 基础命令使用:如df查看磁盘、free查看内存、top监控进程等 系统监控与故障排查:负载判断、端口检查、进程管理等 中间件相关:Nginx状态码、MySQL主从复制、Redis缓存问题 容器技术:Docker优势、Kubernetes作用 运维理念:高可用、监控告警、发布策略、备份容灾 工作原则:故障处理优先级、运维工程师核心能力 文章不仅提供标准答案,还解析了各命令的 阅读详情

相关推荐

如何在INCA中修改标定量的初始值,并刷回控制器

测试过程中,有些时候,标定量来不及修改,这就需要修改标定量的初始值了,又不想打开模型重新编译,因此直接在INCA中修改,然后再刷回控制器,就成了最佳选择,之前也学过,长时间不用,忘记怎么搞了,最近又用到了,这次记录一下,以后用到的时候,就不用在网上东翻西找了。截止到2023年11月24日,共找到了两种方法可以实现修改。

weixin_43406519的博客 4198

leetcode 1007 Minimum Domino Rotations For Equal Row

leetcode 1007 Minimum Domino Rotations For Equal Row1.题目描述2.解题思路3.Python代码 1.题目描述 在一排多米诺骨牌中,A[i] 和 B[i] 分别代表第 i 个多米诺骨牌的上半部分和下半部分。(一个多米诺是两个从 1 到 6 的数字同列平铺形成的 —— 该平铺的每一半上都有一个数字。) 我们可以旋转第 i 张多米诺,使得 A[i] ...

GQxxxxxl的博客 408

Ubuntu22.04 网络图标消失与连接故障排查指南(附实战修复流程)

本文详细介绍了Ubuntu22.04系统中网络图标消失与连接故障的排查与修复方法。通过ifconfig、dhclient等命令诊断问题,提供从紧急恢复网络连接到深度排查的完整解决方案,帮助用户快速恢复网络功能并预防类似问题再次发生。

weixin_28481133的博客 287

三、行相等的最少多米诺旋转(Weekly Contest 127)

题目描述: 当时因为竞赛没有太多考虑,直接用最笨的方法来写的,代码比较冗余 class Solution { public int minDominoRotations(int[] A, int[] B) { //如果A里面的都相等,直接返回0 int first = A[0]; boolean iseq = true; for (int i : A)...

qq_34446716的博客 174

Converting Phase Noise to Random Jitter(Period)

推导了Phase Noise to Random Jitter(Period)的转换过程,解释了分频对Phase Noise & Spur(每2分频改善6dB)的影响,每N分频,TIE RJ(1𝜎)不变(之前已推导),而Period RJ(1𝜎)增加√N倍。

u010486560的博客 2037

微信聊天记录数据库解密实战:从WCDB到sqlcipher4的关键步骤解析

本文详细解析了微信桌面版聊天记录数据库的解密实战过程。通过逆向分析,定位了微信从WCDB框架到sqlcipher4加密的关键步骤,并成功提取了数据库密钥。文章重点介绍了如何通过动态调试、字符串定位等方法,追踪并验证sqlcipher4的加密密钥,最终实现对加密数据库文件的解密操作,为安全研究和数据恢复提供了技术参考。

weixin_29322553的博客 1071

LeetCode1007.Minimum Domino Rotations For Equal Row(行相等的最少多米诺旋转)

1007.Minimum Domino Rotations For Equal Row(行相等的最少多米诺旋转)DescriptionDifficulty: mediumExample 1:Example 2:Note:分析参考代码 Description In a row of dominoes, A[i] and B[i] represent the top and bottom halves...

redtongue的博客 368

Miniconda3迁移(附图详细过程)

服务器上目录占满,想要将conda迁移到其他目录下的操作方法,详细教程。

2301_77554343的博客 6940

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

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法遍历一遍日期 题目地址:https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/ 题目描述 In a row of dominoes, A[i] and B[i] repres...

负雪明烛 3058

OpenClaw赚钱实录:从“养龙虾“到可持续变现的实践指南——OpenClaw的赚钱底层逻辑与“记忆”革命

AI 的商业价值不在于替代思考,而在于沉淀关系。当 Agent 记住你的偏好、你的历史、你的目标,它就不再是冷冰冰的软件,而是成为数字化的关系资产。这种资产的稀缺性不在于技术复杂度,而在于时间无法被压缩——你无法用更好的算法替代 3 个月的协作记忆。在这场“记忆革命”中,护城河不是模型参数,而是用户与 AI 共同书写的历史。OpenClaw 用一套优雅的文件系统架构,将这种历史转化为可积累、可复用、可变现的数字资产。从“计算即服务”(CaaS)到“记忆即服务”(MaaS)。

冯·诺依曼 1341

基于传感器数据实现火灾报警预测分析

火警 系统当检测到烟雾、火灾、一氧化碳或其他与火灾有关或一般通知的紧急情况时,向人们发出警告

DT程序员的博客 721

Minimum Domino Rotations For Equal Row

In a row of dominoes,A[i]andB[i]represent the top and bottom halves of thei-th domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.) We may rotate thei-th d...

leetcode 解题思路 327

【STK】手把手教你利用STK进行光电红外仿真03-STK/EOIR模块属性之二 导弹等目标的EOIR设置

上一节介绍了如何设置传感器的通用属性和空间、光学、光谱、辐射四种专项属性。这一节介绍如果对导弹等目标的EOIR属性进行修改。

weixin_44184852的博客 2203

leetcode 1007. 行相等的最少多米诺旋转(Minimum Domino Rotations For Equal Row)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 在一排多米诺骨牌中,A[i] 和 B[i] 分别代表第 i 个多米诺骨牌的上半部分和下半部分。(一个多米诺是两个从 1 到 6 的数字同列平铺形成的 —— 该平铺的每一半上都有一个数字。) 我们可以旋转第 ...

weixin_30363817的博客 113

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Leetcode-1007 Minimum Domino Rotations For Equal Row(行相等的最少多米诺旋转)

1 class Solution 2 { 3 public: 4 int minDominoRotations(vector<int>& A, vector<int>& B) 5 { 6 int rrnt = INT_MAX; 7 for(i...

weixin_30795127的博客 124

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

leetcode 1007. Minimum Domino Rotations For Equal Row 行相等的最少多米诺旋转-耗时100

Problem:耗时100%,top共6种可能全是1、2、…、6,bottom共6种可能全是1、2、3…所以给定两个数组,分别计数top全是或者bottom全是的翻转次数,而且像n=6,若翻转次数=4,则反过来可以反方向只翻转6-4=2次若 tops[i]!=j 则说明这种情况不可能翻转成全是j,所以标记成-1直接跳过的,若tops[i] == j && bottoms[i]!=j则可以翻转到下面chbottom[j]++;,若bottoms[i]==j && tops[i]!

邹九的个人博客 70

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Leetcode 1007 Minimum Domino Rotations For Equal Row

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 ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.) We may rotate the it

weixin_43796689的博客 404

[leetcode] 1007. Minimum Domino Rotations For Equal Row

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

Learning from the mistakes 185

1007. Minimum Domino Rotations For Equal Row

题目链接:https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/ 解题思路:一开始半天没有思路,看到最小最大之类的,以为要用dp或者贪婪算法,但是依然没思路,然后换种思路,观察,观察什么呢,观察数字特征,发现实际上达到能保证一个数翻转到一侧后能使一侧的数字全一样,这样的数字在A序列和B序列的个数总和必须超过序列长度...

virgilshi的博客 234

【leetcode】1007. Minimum Domino Rotations For Equal Row

题目如下: In a row of dominoes,A[i]andB[i]represent the top and bottom halves of thei-th domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.) We may rotate t...

weixin_33670786的博客 183

LeetCode #1007. Minimum Domino Rotations For Equal Row

题目描述: In a row of dominoes,A[i]andB[i]represent the top and bottom halves of thei-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...

LawFile的博客 174

leetcode 1007 Minimum Domino Rotations For Equal Row & leetcode 1008 Construct Binary Search Tree fr...

leetcode 1007 Minimum Domino Rotations For Equal Row Use three int array of size 6 The first one invalid array records valid(or invalid) numbers which can be the same in A or B the rest two records th...

weixin_30342827的博客 128

(贪心 复习) leetcode 1007. Minimum Domino Rotations For Equal Row

In a row of dominoes,A[i]andB[i]represent the top and bottom halves of thei-th domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.) We may rotate thei...

157

[贪心] leetcode 1007 Minimum Domino Rotations For Equal Row

problem:https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/ 有四种可能性,相等数字为A[0],出现在上面或下面;相等数字为B[0],出现在上面或下面。取这四者最小值即可。 class Solution { public: int n; int min...

ayzejx8547408的博客 199

LeetCode Hot 100 二叉树专题(二)3 题(Java)

本篇文章共 3 题:543. 二叉树的直径、102. 二叉树的层序遍历、108. 将有序数组转换为二叉搜索树。 每道题都统一按「① 题目分析 & 解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结」的顺序组织;代码使用 Java 编写,节点定义沿用 LeetCode 默认的 TreeNode。

2401_88612756的博客 352

LeetCode Hot 100 链表专题 三· 3 题(Java)

收录题目:2. 两数相加、19. 删除链表的倒数第 N 个结点、24. 两两交换链表中的节点。 统一结构:① 题目分析与解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结。 贯穿三题的两条主线:虚拟头结点 dummy(统一处理头结点被修改的场景)与 指针修改顺序(避免断链、丢引用)。

2401_88612756的博客 481
上一篇: leetcode 895. Maximum Frequency Stack(最大频率栈)
下一篇: Bottleneck结构(附代码)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值