▶ 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. ListBox的虚拟可视化技术

    在ListBox中承载大量的数据项时,可采用虚拟可视化技术来提高控件显示数据的性能.如下代码: <ListBox.ItemsPanel>                    <It ...

  2. Learn Rails5.2- ActiveRecord: sqlite3的用法, Query查询语法。乐观锁和悲观锁案例,查询语法includes(), 多态关联,destory和delete, Scope, Validats, Migrations

    rails generate model photo title:string album:references 这会产生一个album_id列,当建立belongs_to关联时,需要用到. refe ...

  3. Java开源-astar:A 星算法

    astar A星算法Java实现 一.适用场景 在一张地图中,绘制从起点移动到终点的最优路径,地图中会有障碍物,必须绕开障碍物. 二.算法思路 1. 回溯法得到路径 (如果有路径)采用“结点与结点的父 ...

  4. poj3734矩阵快速幂

    挑战上面的题目,感觉脑洞很大 分别找红蓝个数全为偶,全为奇,一奇一偶的个数ai,bi,ci 转移矩阵是| 2 1 0 |,是一个对称矩阵(会不会有什么联系.) | 2 2 2 | | 0 1 2 | ...

  5. HIVE之常用字符串函数

    可以参考: 博文 : https://www.iteblog.com/archives/1639.html

  6. Bitwise Equations

    Problem Description You are given two positive integers X and K. Return the K-th smallest positive i ...

  7. Leetcode 34

    //二分查找后,向两边扩展,二分错了两次,现在是对的.//还有就是vector可以用{}直接赋值很棒 class Solution { public: vector<int> search ...

  8. day40 数据结构-算法(二)

    什么是数据结构? 简单来说,数据结构就是设计数据以何种方式组织并存储在计算机中. 比如:列表.集合与字典等都是一种数据结构. N.Wirth: “程序=数据结构+算法” 列表 列表:在其他编程语言中称 ...

  9. [svc]线上Iptables重启报错

    线上iptables重启了下发现报错,排查了下 [root@xxxx ~]# /etc/init.d/iptables restart iptables: Setting chains to poli ...

  10. @Query 注解实现查询(二十四)

    为了节约时间使得各位看官看起来更加简单舒适,这一节把测试方法和测试代码放在一起. 测试方法: // ------------------------------------ 使用 @Query 注解 ...