765. Couples Holding Hands
▶ n 对夫妻共 2n 个人随机坐成一排,“交换其中某两人的位置” 称为一次操作,求最少的操作此次数,使 n 对夫妻两人都相邻。初始座位为非负整数列 D1n-1,其中值为 2k 和 2k+1 的两个元素为一对夫妻。(本体尚未有 Discuss或 Solution)
● 代码,2 ms,顺着梳理
class Solution
{
public:
int minSwapsCouples(vector<int>& row)
{
int i, len;
for (i = len = ; i < row.size(); row[i++] /= ); // 值相等的元素为一对夫妻
for (auto it = row.begin(); it != row.end(); it += ) // 每次调整一对夫妻
{
if (*it != *(it + )) // 只要接下来的两位不是夫妻,则在后面的位置中找到相应的配偶换过来
{
iter_swap(it + , find(it + , row.end(), *it));
len++;
}
}
return len;
}
};
● 代码,4 ms,建立映射表
class Solution
{
public:
void insert_helper(unordered_map<int, int>& m, int v1, int v2)// 接受两个元素的组好,映射表 m 相当于需要调整的次数
{
auto k = min(v1, v2), v = max(v1, v2);
if (k != v)
{
if (m.count(k) > )
insert_helper(m, m[k], v);
else
m[k] = v;
}
return;
}
int minSwapsCouples(vector<int>& row)
{
unordered_map<int, int> m;
for (auto i = ; i < row.size(); i += )
insert_helper(m, row[i] / , row[i + ] / );
return m.size();
}
};
765. Couples Holding Hands的更多相关文章
- [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- ...
- Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands)
Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands) N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并 ...
- [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 ...
- [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 ...
- 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 All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- WPF特效和例子
https://www.cnblogs.com/AaronYang/p/4710428.html
- Android 之低版本高版本实现沉浸式状态栏
沉浸式状态栏确切的说应该叫做透明状态栏.一般情况下,状态栏的底色都为黑色,而沉浸式状态栏则是把状态栏设置为透明或者半透明. 沉浸式状态栏是从android Kitkat(Android 4.4)开始出 ...
- 多个数值转QString
int, float, double等数值类型转换为QString的方法 1. 用QTextStream QTextStream类可以用数据流的方式直接将任意多个数值.字符.字符串等传入QString ...
- bzoj3393
题解: spfa 允许多次进队 代码: #include<bits/stdc++.h> using namespace std; struct que{int x,y,dire,dist; ...
- 12.21 Gson的常用用法 功能介绍 特点
使用谷歌GSON常用语法: 功能:映射Java Object与json格式的数据 1.通过Annotation注解来声明 2.支持自定义属性名称 3.支持包含或排除属性 4.支持自定义接口自己完成解析 ...
- 【LeetCode 36_哈希表】Valid Sudoku
//occupyed_1检查行是否占用 //occupyed_2检查列是否占用 //occupyed_3检查块是否占用 bool isValidSudoku(vector<vector<c ...
- 在mysql中,如何改变列声明.
C 在mysql中,如何改变列声明. 修改表 - 修改列名 使用 CHANGE COLUMN 来修改列的名字,还必须 设置 列的数据类型 mysql> desc test_tab -> / ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- linux 服务器性能调优总结
1.性能分析的几个方面 https://blog.csdn.net/w174504744/article/details/53894127 2.cpu 性能分析工具 perf https://blog ...
- Coding(码市)教程(一):基础配置
作者:Adaaaagio 出处:http://www.cnblogs.com/zyx418 欢迎转载,希望能够帮到更多的人,转载也请保留这段申明,谢谢! 初识coding是在新入职的公司,前辈说我们用 ...