This page looks best with JavaScript enabled

Leetcode 1947. Maximum Compatibility Score Sum

 ·  ☕ 2 min read

Concept

先說結論:
1.Backtracking
2.Permutation

1.Backtracking

因為看到Constraints: 1 <= m, n <= 8
在比賽當下第一個想到的是用backtracking
計算次數: m*m*n
先把所有的可能算出存到一個table(避免在backtracking時重複計算)
table
(這畫圖技術…)
那這樣就可以把此問題轉為一個類似N-queens的問題
找出所有路徑並把所有答案記錄下來,算出最大值
table_path

2.Permutation

透過Backtracking觀察
我們可以發現所有路徑就是 0 ~ n-1 的permutation
一樣透過存值到table
將這些路徑全部算出即可得到答案
hints: C++ <alogrithm>中有個next_permutation可以用

Code

Backtracking

Runtime: 52 ms,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public:
    int ans = INT_MIN;
    int bit_count(vector<int>& a, vector<int>& b){
        int count = 0;
        for(int i = 0; i < a.size(); i++){
            if(a[i] == b[i]){
                count++;
            }
        }
        return count;
    }
    void helper(vector<vector<int>>& s, vector<vector<int>>& m, vector<vector<int>>& table, vector<bool>& visited, int pos, int count){
        if(pos == s.size()){
            ans = max(ans, count);
            return;
        }
        for(int i = 0; i < table.size(); i++){
            if(visited[i]){
                visited[i] = false;
                helper(s, m, table, visited, pos + 1, count + table[pos][i]);
                visited[i] = true;
            }
        }
    }
    int maxCompatibilitySum(vector<vector<int>>& students, vector<vector<int>>& mentors) {
        int bits = students[0].size();
        int n = students.size();
        vector<vector<int>> table(n, vector<int>(n, 0));
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                table[i][j] = bit_count(students[i], mentors[j]);
            }
        }
        vector<bool> visited(n, true);
        helper(students, mentors, table, visited, 0, 0);
        return ans;
    }
};

Permutation without pre-calculate table

Runtime: 384 ms

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
    int maxCompatibilitySum(vector<vector<int>>& students, vector<vector<int>>& mentors) {
        int ans = 0;
        vector<int> list;
        int n = students.size();
        int m = students[0].size();
        for(int i = 0; i < n; i++) {
            list.push_back(i);
        }
        do{
            int count = 0;
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    if(students[i][j] == mentors[list[i]][j]){
                        count ++;
                    }
                }
            }
            ans = max(ans, count);
        }while(next_permutation(list.begin(), list.end()));
        return ans;
    }
};

Permutation with pre-calculate table

Runtime: 52ms

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
    int bit_count(vector<int>& a, vector<int>& b){
        int count = 0;
        for(int i = 0; i < a.size(); i++){
            if(a[i] == b[i]){
                count++;
            }
        }
        return count;
    }
    
    int maxCompatibilitySum(vector<vector<int>>& students, vector<vector<int>>& mentors) {
        int ans = 0;
        vector<int> list;
        int n = students.size();
        int m = students[0].size();
        vector<vector<int>> table(n, vector<int>(n, 0));
        
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                table[i][j] = bit_count(students[i], mentors[j]);
            }
        }
        for(int i = 0; i < n; i++) {
            list.push_back(i);
        }
        do{
            int count = 0;
            for(int i = 0; i < n; i++){
                count += table[i][list[i]];
            }
            ans = max(ans, count);
        }while(next_permutation(list.begin(), list.end()));
        return ans;
    }
};
Share on

Bill Zhong
WRITTEN BY
Bill Zhong
Software Engineer