▶ 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. Mongo Plugin插件(编辑器PyCharm的Mongo插件安装与使用)

    博主接触到MongoDB数据库.用普通的Navicat工具 是不支持的 正准备重新安装一款对应的可视化工具.刚好发现在PyCharm编辑中有连接mongoDB数据的插件 Mongo Plugin 这里 ...

  2. 如何以Root权限在Pycharm上Run、Debug

    Pycharm官网提问:https://intellij-support.jetbrains.com/hc/en-us/community/posts/206587695-How-to-run-deb ...

  3. JSP Cookies 处理

    JSP Cookies 处理 Cookies是存储在客户机的文本文件,它们保存了大量轨迹信息.在servlet技术基础上,JSP显然能够提供对HTTP cookies的支持. 通常有三个步骤来识别回头 ...

  4. Rails 5 Test Prescriptions 第10章 Testing for Security

    Web 安全是一个可怕的主题.所有的你的程序都依靠密码学,代码超出了你的控制. 尽管如此,你还是可以控制部分网页安全 --所有的logins和access checks和injection error ...

  5. Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

    Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...

  6. Facebook的工程师文化——《打造facebook》读后感

    在今年北京的QCon大会上听了facebook早期中国籍工程师王淮的演讲,受益匪浅,主题是如何打造高效能团队,主要介绍他在facebook的一些经历和管理上的经验分享.现在的他是一名天使投资人,投资的 ...

  7. 保卫萝卜官方PC版——含绿色版 V1.0.6Beta

    官方网站 | 安装版 | 绿色版

  8. B树、B-树、B+树、B*树都是什么

    B树.B-树.B+树.B*树都是什么 B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右 ...

  9. 设计模式(Python)-观察者模式

    本系列文章是希望将软件项目中最常见的设计模式用通俗易懂的语言来讲解清楚,并通过Python来实现,每个设计模式都是围绕如下三个问题: 为什么?即为什么要使用这个设计模式,在使用这个模式之前存在什么样的 ...

  10. RabbitMQ消息队列安装

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