1. 程式人生 > >演算法設計與分析(五):Graph And Tree

演算法設計與分析(五):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;
    }
    };