当前位置: 首页 > ai >正文

力扣每日一题1007、行相等的最少多米诺旋转

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

        在一排多米诺骨牌中,tops[i] 和 bottoms[i] 分别代表第 i 个多米诺骨牌的上半部分和下半部分。(一个多米诺是两个从 1 到 6 的数字同列平铺形成的 —— 该平铺的每一半上都有一个数字。)

我们可以旋转第 i 张多米诺,使得 tops[i] 和 bottoms[i] 的值交换。

返回能使 tops 中所有值或者 bottoms 中所有值都相同的最小旋转次数。

如果无法做到,返回 -1.

思考!显然所有情况只会有四种:

①以tops[0]为首,bottoms中和其相等的全部翻转上来,记录次数,如果有一个存在上下都没有,则此次记录作废

②以bottoms[0]为首,tops中和其相等的全部翻转下来,记录次数,如果有一个存在上下都没有,则此次记录作废

③先将top[0]交换到下面,然后重复上面操作

④先将bottoms[0]交换到上面,然后重复上述操作。

实现:

C++:

class Solution {
public:int Rotations(vector<int>& tops,vector<int>& bottoms,int target){int toUp = 0;int toBottom=0;for(int i = 0;i<tops.size();i++){int x = tops[i];int y = bottoms[i];if(x!=target&&y!=target){return INT_MAX;}if(x!=target){//上面的不满足条件//那么下面的一定满足条件 不然在上面就会返回一个最大值了toUp++;}else if(y!=target){toBottom++;}}return min(toUp,toBottom);}int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {int ans = min(Rotations(tops,bottoms,tops[0]),Rotations(tops,bottoms,bottoms[0]));return ans ==INT_MAX?-1:ans;}
};

C#:

public class Solution {//目标函数 计算翻转次数//target为目标比较对象public int Rotations(int[] tops,int[] bottoms,int target){//翻转次数(从上往下)int topToButtom = 0;//翻转次数(从下往上)int buttomToTop = 0;for(int i = 0;i<tops.Length;i++){//拿出来比较int x = tops[i];int y = bottoms[i];if(x!=target&&y!=target){return int.MaxValue;}if(x!=target){buttomToTop++;}else if(y!=target){topToButtom++;}}return Math.Min(buttomToTop,topToButtom);}public int MinDominoRotations(int[] tops, int[] bottoms) {int ans = Math.Min(Rotations(tops,bottoms,tops[0]),Rotations(tops,bottoms,bottoms[0]));return ans ==int.MaxValue?-1:ans;}
}

不写函数,直接实现:

C++:

class Solution {
public:int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {int ans = INT_MAX;for (int i = 1; i <= 6; i++) {int toUp = 0;int toBottom = 0;bool isValid = true;for (int j = 0; j < tops.size(); j++) {if (tops[j] != i && bottoms[j] != i) {isValid = false;break;}if (tops[j] != i)toUp++;if (bottoms[j] != i)toBottom++;}if (isValid) {ans = min(toBottom, toUp);}}return ans == INT_MAX ? -1 : ans;}
};

C#:

public class Solution {public int MinDominoRotations(int[] tops, int[] bottoms) {int minRotations = int.MaxValue;// 遍历所有可能的候选值(1到6)for (int candidate = 1; candidate <= 6; candidate++) {int topRotations = 0, bottomRotations = 0;bool validCandidate = true;for (int i = 0; i < tops.Length; i++) {// 检查当前候选值是否存在于当前骨牌的两个面中if (tops[i] != candidate && bottoms[i] != candidate) {validCandidate = false;break;}// 计算将tops或bottoms全变为candidate所需的旋转次数if (tops[i] != candidate) topRotations++;if (bottoms[i] != candidate) bottomRotations++;}if (validCandidate) {// 取两种目标(tops全为candidate或bottoms全为candidate)的较小值minRotations = Math.Min(minRotations, Math.Min(topRotations, bottomRotations));}}return minRotations == int.MaxValue ? -1 : minRotations;}
}

http://www.xdnf.cn/news/3739.html

相关文章:

  • 爬虫管理平台-最新版本发布
  • 李沐《动手学深度学习》 | Softmax回归 - 分类问题
  • 【AI面试准备】从0-1搭建人工智能模型自动化评估理论与测试,掌握测试数据集建立与优化,熟练数据处理和模型评测工作
  • RV1126单目摄像头取流,实现双路输出(一路H.264编码推流,一路给算法)
  • 【React】 Hooks useTransition 解析与性能优化实践
  • 套接字+Socket连接
  • Y1模拟一 补题报告
  • function包装器的意义
  • Milvus(13):自定义分析器、过滤器
  • Dubbo(94)如何在金融系统中应用Dubbo?
  • validator - Go 结构体验证库
  • 每天五分钟深度学习框架PyTorch:基于Dataset封装自定义数据集
  • 深入理解Java垃圾回收机制
  • NV228NV254固态美光颗粒NV255NV263
  • 2025年01月03日美蜥(杭州普瑞兼职)一面
  • 【C++ Qt】输入类控件(上) LineEdit、QTextEdit
  • 升级 CUDA Toolkit 12.9 与 cuDNN 9.9.0 后验证指南:功能与虚拟环境检测
  • 黑马点评day01(基于Redis)
  • C++学习:六个月从基础到就业——C++11/14:右值引用与移动语义
  • Webug4.0靶场通关笔记14- 第18关 文件上传之Nginx解析缺陷
  • Linux线程深度解析:从基础到实践
  • 码蹄集——偶数位、四边形坐标
  • 南京优质的公司有哪些?
  • 小程序 IView WeappUI组件库(简单增删改查)
  • 变更需求代价:影响分析
  • Java面试大纲(以及常见面试问答)
  • 19、权限控制:分院帽系统——React 19 RBAC实现
  • iview自定义下拉树菜单
  • 【C++】 —— 笔试刷题day_25
  • C++多态(下)