public class Solution
{
public int MinDominoRotations(int[] A, int[] B)
{
var na = A.Length;
var nb = B.Length;
if (na != nb)
{
return -;
}
var dic1 = new Dictionary<int, List<int>>();
var dic2 = new Dictionary<int, List<int>>();
for (var i = ; i <= ; i++)
{
dic1.Add(i, new List<int>());
dic2.Add(i, new List<int>());
} for (var i = ; i < na; i++)
{
var a = A[i];
dic1[a].Add(i); var b = B[i];
dic2[b].Add(i);
} var minchanges = na;
var find = false;
for (var i = ; i <= ; i++)
{
var top = dic1[i];
var bottom = dic2[i];
var temp = top.Union(bottom).Distinct();
if (temp.Count() == na)
{
minchanges = Math.Min(minchanges, na - Math.Max(top.Count(), bottom.Count()));
find = true;
}
}
return find ? minchanges : -;
}
}

思路,分别记录上部和下部的每一个点数出现的索引,然后对以此判断每一个点数的并集,是否包含了全部的索引。

这样的点数,就是可以满足在同一侧全是一样的点数。

下面就要找最小的移动次数,发现最小的移动次数是保持这个点数出现的次数多的一面不动,而把这个点数出现在另一面的牌进行翻转。

如代码所示:34行,求并集。37行,进行最小次数判断。

leetcode1007的更多相关文章

  1. [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 ...

随机推荐

  1. Building the Unstructured Data Warehouse: Architecture, Analysis, and Design

    Building the Unstructured Data Warehouse: Architecture, Analysis, and Design earn essential techniqu ...

  2. pyqt4 利用信号槽在子线程里面操作Qt界面

    转载:ABigCaiBird #-*- coding:utf-8 -*- ####### from PyQt4.QtCore import * from PyQt4.QtGui import * im ...

  3. vc++使用IWinHttpRequest获取网页内容乱码

    mfc项目的字符集为unicode字符集 乱码前代码: CString rspStr = pHttpReq->ResponseText; MessageBox(rspStr); 乱码效果: 解决 ...

  4. Java工程师 必须掌握的知识点

    Web核心:XML.HTTP及Tomcat.Servlet.request与response.cookie与session.jsp技术.jdbc高级.Ajax开发.Filter/Listener高级. ...

  5. hashMap为啥初始化容量为2的次幂

    原文 https://blog.csdn.net/sd_csdn_scy/article/details/57083619hashMap源码获取元素的位置: static int indexFor(i ...

  6. AD中组的概念

  7. js浮点数运算封装, 起因财务部分精确计算

    目录 背景 具体代码 背景 项目中用到浮点数,Int 等 js中 Number类型比较多, 加上牵涉到财务软件, 前台js运算等. 有时候会出现精确度的问题 , 公共方法中有好事者写的方法. 此处拿来 ...

  8. 第2课 类型推导(2)_decltype关键字

    1. decltype关键字 (1)auto所修饰的变量必须被初始化,编译器才能通过初始化来确定auto所代表的类型,即必须先定义变量. (2)decltype可以在编译期推导出一个变量或表达式的结果 ...

  9. insert into table 插入多条数据

    方法1: insert into `ttt` select '001','语文' union all select '002','数学' union all select '003','英语'; 方法 ...

  10. 图片Alpha预乘的作用[转]

    Premultiplied Alpha 这个概念做游戏开发的人都不会不知道.Xcode 的工程选项里有一项 Compress PNG Files,会对 PNG 进行 Premultiplied Alp ...