算法设计与分析(五):Graph And Tree

Couples Holding Hands

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.

要求使得一对夫妇(即编号为2N和2N+1的两人)做到一起的交换次数最少的交换次数。
咋看要求这种最优交换方法情况可能很复杂,其实最平常的交换方法就是最优的,即:一对一对地向前搜索,每遇到一对不匹配的,找到队列中那个匹配的,与不匹配的交换位置即可。

依照这个思路,我们可以设计算法:
两个一组地向前遍历数组,将每次取出的两个值取出比较,如果这组值是一对夫妇

(row[i] % 2 == 0 && row[i+1] - row[i] == 1) || (row[i] % 2 != 0 && row[i] - row[i+1] == 1)

那么继续向前,如果不匹配,则在数组中搜索与组中第一人匹配的夫妇,将其与组中第二人交换位置,记一次交换

for(int j = i+2;j < row.size() ; j++) {
     if((row[i] % 2 == 0 && row[j] - row[i] == 1) || (row[i] % 2 != 0 && row[i] - row[j] == 1)) {
     swap(row[i+1],row[j]);
     ans ++;
     }

该算法的时间复杂度为O(nlogn)。
之后我在Discuss中,找到了一种更优秀的算法:

创建两个数组:

vector<int> ptn(N, 0);
vector<int> pos(N, 0);

其中ptn[n]代表了某个n的伙伴的编号,pos[n]代表了n在数组中的位置。
对于数组中i处的一次交换,我们要做的就是:

  1. 求出i的伙伴的编号 ptn[row[i]]
  2. 求出伙伴所在的位置 pos[ptn[row[i]]]
  3. 求出伙伴所在位置的人的伙伴 ptn[pos[ptn[row[i]]]],即为i位置上的人应该在的位置
  4. 将这两个位置上的人交换

这种算法的时间复杂度为O(n).

最后附上两种算法的完整代码:

 class Solution {
public:
    int minSwapsCouples(vector<int>& row) {
        int ans = 0;
        for(int i = 0; i < row.size()-1 ; i = i + 2) {
            if((row[i] % 2 == 0 && row[i+1] - row[i] == 1) || (row[i] % 2 != 0 && row[i] - row[i+1] == 1)) continue;
            else {
                for(int j = i+2;j < row.size() ; j++) {
                    if((row[i] % 2 == 0 && row[j] - row[i] == 1) || (row[i] % 2 != 0 && row[i] - row[j] == 1)) {
                        swap(row[i+1],row[j]);
                        ans ++;
                    }
                }
            }
        }
        return ans;
    }
};

    class Solution {
    public:
    int minSwapsCouples(vector<int>& row) {
        int ans = 0, N = row.size()
        vector<int> ptn(N, 0);
        vector<int> pos(N, 0);
            
       for (int i = 0; i < N; i++) {
            ptn[i] = (i % 2 == 0 ? i + 1 : i - 1);
            pos[row[i]] = i;
        }
        
       for (int i = 0; i < N; i++) {
            for (int j = ptn[pos[ptn[row[i]]]]; i != j; j = ptn[pos[ptn[row[i]]]]) {
    	        swap(row[i], row[j]);
                swap(pos[row[i]], pos[row[j]]);
    	        ans++;
    	    }
        }
            
       return ans;
    }
    };

附上LeetCode链接:https://leetcode.com/problems/couples-holding-hands/description/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值