ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2021-02-14

2021-02-14 23:58:49  阅读:208  来源: 互联网

标签:02 14 int len being range 情侣 2021 row


2021-02-14 Leetcode每日刷题

题目

N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum number of swaps so that every couple is sitting side by side. A swap consists of choosing any two people, then they stand up and switch seats.

The people and seats are represented by an integer from 0 to 2N-1, the couples are numbered in order, the first couple being (0, 1), the second couple being (2, 3), and so on with the last couple being (2N-2, 2N-1).
The couples’ initial seating is given by row[i] being the value of the person who is initially sitting in the i-th seat.

Example 1:

Input: row = [0, 2, 1, 3]
Output: 1
Explanation: We only need to swap the second (row[1]) and third (row[2]) person.

Example 2:

Input: row = [3, 2, 0, 1]
Output: 0
Explanation: All couples are already seated side by side.

Note:

len(row) is even and in the range of [4, 60].
row is guaranteed to be a permutation of 0…len(row)-1.

我的思路
是看了解答的贪心策略才敢写的。对于每组数字(2i, 2i+1),当第一个数为偶数的时候检查第二个数是否比它大1,如果不是则搜索到之后交换一次。当第一个数为奇数的时候检查第二个数是否比它小1并进行交换。我也不知道这样为什么是对的,但是它的答案确实是对的。

class Solution:
    def minSwapsCouples(self, row: List[int]) -> int:
        cnt = 0
        for i in range(int(len(row)/2)):
            if row[2*i]%2 == 0:
                if row[2*i+1] != row[2*i] +1:
                    for j in range(2*i+1,len(row)):
                        if row[j] == row[2*i]+1:
                            row[2*i+1],row[j] = row[j],row[2*i+1]
                            cnt+=1 
            else:
                if row[2*i+1] != row[2*i] - 1:
                    for j in range(2*i+1,len(row)):
                        if row[j] == row[2*i]-1:
                            row[2*i+1],row[j] = row[j],row[2*i+1]
                            cnt+=1
        return cnt

提交结果
在这里插入图片描述
参考思路
关于为什么贪心解法是正确的:

  • 从图的角度来讲,将N对情侣看做图中的 N 个节点;对于每对相邻的位置,如果是第i对与第j对坐在了一起,则在i号节点与j号节点之间连接一条边,代表需要交换这两对情侣的位置。

  • 因为这题情侣是2人一对,同一连通分量内,每个点最多连出两条边,且必成环(可以用反证法;当连通分量内只有一个点时,说明该情侣坐对了,不需要换位置;只有两个点时,图中只有一条边,直接调换一次即可;在一般情况下,即连通分量内点数大于2时,此时每个点必须连出两条边[如果只连一条边的话,一定属于第二种只有两个点的情况;连三条边不可能,因为情侣是2人]!这一条件当且仅当成环的时候成立,反之会出现某些点只有一条边或者多余两条边的情况)。

  • 至此,对于每个连通分量,由于其一定成环,所以我们直接沿着环的方向依次交换(边数-1)次即可使得该连通分量内情侣牵手,也就是贪心交换。

一个简化版的代码:

class Solution(object):
    def minSwapsCouples(self, row):
        """
        :type row: List[int]
        :rtype: int
        """
        N = len(row)
        res = 0
        for i in range(0, N - 1, 2):
            if row[i] == row[i + 1] ^ 1:
                continue
            for j in range(i + 1, N):
                if row[i] == row[j] ^ 1:
                    row[i + 1], row[j] = row[j], row[i + 1]
            res += 1
        return res

这里有个小技巧合并奇数偶数两种情况:

求数字 x 的对象时用到了一个技巧,x 的对象是x ^ 1。解释如下:

  • 当 x 是偶数,则其二进制的末尾是 0,所以 x ^ 1 将其二进制的末尾改成 1,于是得到了x的对象 x + 1。 当 x
  • 是奇数,则其二进制的末尾是 1,所以 x ^ 1 将其二进制的末尾改成 0,于是得到了x的对象 x - 1。

情人节算这个好心累。祝天下有情人终成眷属。
正好一周了,从明天开始加大力度多刷一道简单题。

标签:02,14,int,len,being,range,情侣,2021,row
来源: https://blog.csdn.net/weixin_43296552/article/details/113812276

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有