1947. Maximum Compatibility Score Sum(DFS/DP)

This question is quite straightforward, Just need to find all combinations and calculate the score using dfs.

This is a brute force, and remember the dfs method we need to pass the value by reference otherwise it will use the extra time to copy the vector and parameter which would cause TLE.

class Solution {
public:
    int ans = 0;
    bool visited[10] = {0};
    int dp[100][100] = {0};
    int checkScore(vector<int> &s, vector<int> &m){
        int cnt = 0;
        for(int k = 0; k < s.size(); k++){
            if(s[k] == m[k]) cnt++;
        }
        return cnt;
    }
    void dfs(vector<vector<int>> & m, vector<vector<int>> & s, int idx, int score){
        if(idx == s.size()){
            ans = max(ans, score);
            return;
        }
        for(int i = 0; i < m.size(); i++){
            if(visited[i])continue;
            visited[i] = 1;
            int n = dp[idx][i];
            dfs(m, s, idx + 1, score + n);
            visited[i] = 0;
        }
    }
    int maxCompatibilitySum(vector<vector<int>>& s, vector<vector<int>>& m) {
        //brute force match all
        for(int i = 0; i < s.size(); i++){
            for(int j = 0; j < m.size(); j++){
                dp[i][j] = checkScore(s[i], m[j]);
            }
        }
        dfs(m, s, 0, 0);
        return ans;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>