LeetCode765. Couples Holding Hands
N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum number of swaps so that every couple is sitting side by side. A swap consists of choosing any two people, then they stand up and switch seats.
The people and seats are represented by an integer from 0 to 2N-1, the couples are numbered in order, the first couple being (0, 1), the second couple being (2, 3), and so on with the last couple being (2N-2, 2N-1).
The couples' initial seating is given by row[i] being the value of the person who is initially sitting in the i-th seat.
Example 1:
Input: row = [0, 2, 1, 3]
Output: 1
Explanation: We only need to swap the second (row[1]) and third (row[2]) person.
Example 2:
Input: row = [3, 2, 0, 1]
Output: 0
Explanation: All couples are already seated side by side.
Note:
len(row)is even and in the range of[4, 60].rowis guaranteed to be a permutation of0...len(row)-1.
思路
本来以为是用dp解题,然而不是,还是好好背题吧。解法有 cyclic swapping,并差集,贪心这三种。
一大串英文解释,看的不是很懂,百度了下,看到一片篇解释得很易懂的博文:
https://www.cnblogs.com/grandyang/p/8716597.html
首先是贪心的解法:
class Solution {
public int minSwapsCouples(int[] row) {
int res=0, n=row.length;
for(int i=0;i<n;i=i+2){
if(row[i+1]==(row[i]^1)) continue;
res++;
for(int j=i+1;j<n;j++){
if(row[j]==(row[i]^1)){ // 这里注意要加括号,因为java中恒等运算符的优先级大于位运算
row[j]=row[i+1];
row[i+1]=(row[i]^1);
break;
}
}
}
return res;
}
}
接下来是并查集的解法,关于并查集算法的解释可以见这篇博文:https://blog.csdn.net/dm_vincent/article/details/7655764
LeetCode上的解释:
Think about each couple as a vertex(顶点) in the graph. So if there are N couples, there are N vertices. Now if in position 2i and 2i +1 there are person from couple u and couple v sitting there, that means that the permutations are going to involve u and v. So we add an edge to connect u and v. The min number of swaps = N - number of connected components. This follows directly from the theory of permutations. Any permutation can be decomposed into a composition of cyclic permutations. If the cyclic permutation involve k elements, we need k -1 swaps. You can think about each swap as reducing the size of the cyclic permutation by 1. So in the end, if the graph has k connected components, we need N - k swaps to reduce it back to N disjoint vertices.
class Solution {
private class UF {
private int[] parents;
public int count;
UF(int n) { // 初始化组号
parents = new int[n];
for (int i = 0; i < n; i++) {
parents[i] = i; // i-具体节点的值,parents[i]-节点i所对应的组号,放在这题中i就是couple的编号,数组值就是这个couple应该在的组号
}
count = n;
}
private int find(int i) {
if (parents[i] == i) { // 如果couple的编号和组号对应,所在组号正确,直接返回组号
return i;
}
parents[i] = find(parents[i]); // 这种情形时发生了标记1的情况,连接后组号被修改过,不会和原来对应
return parents[i];
}
public void union(int i, int j) {
int a = find(i);
int b = find(j);
if (a != b) { // 如果不在一个组,连接之
parents[a] = b; // 将a的组号改成b的,注意原parents数组如果组号是a,那么其数组索引也是a。标记1
count--;
}
}
}
public int minSwapsCouples(int[] row) {
int N = row.length/ 2;
UF uf = new UF(N); // 并查集初始化组号
for (int i = 0; i < N; i++) {
int a = row[2*i];
int b = row[2*i + 1];
uf.union(a/2, b/2);
}
return N - uf.count;
}
}
LeetCode765. Couples Holding Hands的更多相关文章
- Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands)
Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands) N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并 ...
- [Swift]LeetCode765. 情侣牵手 | Couples Holding Hands
N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...
- [LeetCode] Couples Holding Hands 两两握手
N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...
- [LeetCode] 765. Couples Holding Hands 情侣牵手
N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...
- 【LeetCode】765. Couples Holding Hands 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/couples- ...
- 765. Couples Holding Hands
▶ n 对夫妻共 2n 个人随机坐成一排,“交换其中某两人的位置” 称为一次操作,求最少的操作此次数,使 n 对夫妻两人都相邻.初始座位为非负整数列 D1n-1,其中值为 2k 和 2k+1 的两个元 ...
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 一些平台无关的整型类型,int8_t,uint8_t....
pecific integral type limits Specifier Common Equivalent Signing Bits Bytes Minimum Value Maximum Va ...
- python基础----__slots__方法、__call__方法
''' 1.__slots__是什么:是一个类变量,变量值可以是列表,元祖,或者可迭代对象,也可以是一个字符串(意味着所有实例只有一个数据属性) 2.引子:使用点来访问属性本质就是在访问类或者对象的_ ...
- C#调用GDI+1.1中的函数实现高斯模糊、USM锐化等经典效果。
http://www.cnblogs.com/Imageshop/archive/2012/12/13/2815712.html 在GDI+1.1的版本中,MS加入不少新的特性,其中的特效类Effec ...
- Codeforces Round #415 (Div. 2) A B C 暴力 sort 规律
A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- poj1006 生理周期
生理周期 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 138947 Accepted: 44597 Descripti ...
- 限制SSH远程登录用户仅能只读访问Linux中指定的目录
资料参考:http://os.51cto.com/art/201703/534895.htm 背景需求: 在TOMCAT服务器上建立一个普通帐号log_user,只能查看TOMCAT日志,不能删改任何 ...
- 这年头不会点Git真不行!!!
版本控制 说到版本控制,脑海里总会浮现大学毕业是写毕业论文的场景,你电脑上的毕业论文一定出现过这番景象! 1 2 3 4 5 6 7 8 9 10 11 毕业论文_初稿.doc 毕业论文_修改1.do ...
- svn常见错误
1.svn提交报错:svn: Aborting commit:XXXXXremains in conflict 解决:说明Svn服务器上的对应内容,在你上次Update后已被别人修改了,而你也做了修改 ...
- hdu 1907 John (anti—Nim)
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)http://acm.h ...
- 【BZOJ】1497: [NOI2006]最大获利 最大权闭合子图或最小割
[题意]给定n个点,点权为pi.m条边,边权为ci.选择一个点集的收益是在[点集中的边权和]-[点集点权和],求最大获利.n<=5000,m<=50000,0<=ci,pi<= ...