▶ 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的更多相关文章

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

  2. 【LeetCode】765. Couples Holding Hands 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/couples- ...

  3. Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands)

    Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands) N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并 ...

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

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

  6. 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 ...

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. 【LeetCode 36_哈希表】Valid Sudoku

    //occupyed_1检查行是否占用 //occupyed_2检查列是否占用 //occupyed_3检查块是否占用 bool isValidSudoku(vector<vector<c ...

  2. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 08

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2389] 版本控制:https://git.coding.net/liuyy08 ...

  3. jQuery插件开发——全屏切换插件

    这个插件包含三个部分:HTML结构.CSS代码和JS代码. HTML结构是固定的,结构如下: <!--全屏滚动--> <div class="fullpage-contai ...

  4. Nginx的负载均衡和高可用

    一.Nginx的理解 Nginx是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务.Nginx是一款轻量级的web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理 ...

  5. linux 下的php_gd2.dll

    今天写验证码时,发现要配置php.ini,单不知在哪儿,用下面的办法可以解决. <?php phpinfo(); ?> 用vim搜索字符串时,发现一个命令特别好用 /字符串 就可以搜索到字 ...

  6. 重新学习之spring第四个程序,整合struts2+hibernate+spring

    第一步:导入三大框架的jar包(struts2.3.16.1+hibernate3.2+spring3.2.4) 第二步:编写web.xml 和struts.xml和applicationContex ...

  7. 494 - Kindergarten Counting Game

     Kindergarten Counting Game  Everybody sit down in a circle. Ok. Listen to me carefully. ``Woooooo, ...

  8. RabbitMQ消息队列安装

    [root@VM_119_179_centos ~]# rpm -ivh erlang-19.0.4-1.el6.x86_64.rpm [root@VM_119_179_centos ~]# rpm ...

  9. SpringBoot2.0 url中出现特殊符号「带括号{}'"等等」时会抛出400错误

    访问 http://127.0.0.1:8080/api?method=taxiong.goods.list&params={"page":1,"pageSize ...

  10. 在vc++上简单搭建环境(包括文件引用)

    1,triplet_head.h 文件 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVER_FLOW -2 ...