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 ...
随机推荐
- 【Android】冷门常用 ADB
清除应用缓存adb shell pm clear 包名 获取手机中安装的包名,加上部分包名可以做筛选 adb shell pm list package adb shell pm list packa ...
- java--Quartz 定时执行
第一步:引包(Maven) <!-- 定时任务 --> <dependency> <groupId>org.quartz-scheduler</groupId ...
- 将C语言的CRC32 代码转成JAVA的CRC32 代码
public class CustomerCRC32 { private static long[] crc32Table = new long[256]; static { long crcValu ...
- BZOJ1228 [SDOI2009]E&D
蒟蒻不会= = 话说写题解的巨巨也只会打表233 反正先A掉再说 /************************************************************** Pro ...
- 进程间通信IPC
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- Fedora BCM43142 无线网卡驱动安装
OS:Fedora 25 KDE 系统内核:4.10.16-200.fc25.x86_64 #1 网卡:BCM43142 1.识别自己的网卡型号:命令:lspci | grep -i broadcom ...
- 在jenkins和sonar中集成jacoco(四)--在sonar中集成jacoco
首先要得到之前的单元测试和集成测试的覆盖率文件,还有对应的class文件以及单元测试的覆盖率报告,材料准备齐全之后,使用如下命令: build.xml 1 2 3 4 5 6 7 8 9 10 11 ...
- kmp算法中的next数组实例解释
假设求串′ababaaababaa′的next数组 模式串 a b a b a a a b a b a a 下标 1 2 3 4 5 6 7 8 9 10 11 12 1.前两位:next数组前两位一 ...
- php http build query
http_build_query (PHP 5, PHP 7) http_build_query — 生成 URL-encode 之后的请求字符串 说明¶ string http_build_quer ...
- 编写高质量代码 改善Python程序的91个建议 (读后 小记)
此书是自己好久之前买的,当时总觉得Python语言中有各种trick, 总是要自己猝不及防的掉入到陷阱之中, 看了一些资料后发现了这本书,感觉很是不错,不过可惜自己平时总是杂事太多,总是找不到整块的时 ...