【LeetCode】第 345 场周赛题解

28 阅读1分钟

6430. 找出转圈游戏输家

题解:模拟

class Solution {
public:
    vector<int> circularGameLosers(int n, int k) {
       vector<int> p(n, 0);
       int pre = 0;
       p[0] = 1;
       for(int i=1; i<=n; i++){
           int idx = (pre+i*k)%n; 
           if(p[idx] > 0){
               break;
           }
           p[idx]++;
           pre = idx;
       }
       vector<int> res;
       for(int i=0; i<n; i++)
            if(p[i] == 0) res.push_back(i+1);
       return res;
    }
};

6431. 相邻值的按位异或

题解:位运算

class Solution {
public:
    bool doesValidArrayExist(vector<int>& d) {
        int n = d.size();
        vector<int> a(n+1), b(n+1);
        a[0] = 0, b[0] = 1;
        for(int i=0; i<n; i++){
            a[i+1]=a[i]^d[i];
            b[i+1]=b[i]^d[i];
        }
        if(a[0]^a[n-1]!=d[n-1] && b[0]^b[n-1]!=d[n-1]){
            return false;
        }
        return true;
    }
};

6433. 矩阵中移动的最大次数

题解:DFS

class Solution {
public:
    vector<vector<int>> g;
    int n, m;
    unordered_map<string, int> hash;
    bool check(int i, int j){
        return i>=0 && j>=0 && i<n && j < m;
    }
    int dir[3][2]={{-1, 1}, {0, 1}, {1,1}};
    int dfs(int i, int j, int d){
        string key=to_string(i)+"_"+to_string(j);
        if(hash.count(key)){
            return hash[key];
        }
        int ret = 0;
        for(auto v : dir){
            int x = i+v[0];
            int y = j+v[1];
            if(check(x, y) && g[x][y] > g[i][j]){
               ret = max(dfs(x, y, d)+1, ret);
            }
        }
        return  hash[key] = ret;
    }
    int maxMoves(vector<vector<int>>& grid) {
        g = grid;
        n = grid.size(), m = grid[0].size();
        int res = 0;
        for(int i=0; i<n; i++){
            res = max(res, dfs(i, 0, 0));
        }
        return res;     
    }
};

6432. 统计完全连通分量的数量

题解:BFS

class Solution {
public:
    bool vist[55];
    int countCompleteComponents(int n, vector<vector<int>>& edges) {
        memset(vist, false, sizeof vist);
        unordered_map<int, vector<int>> g;
        for(auto e : edges){
            g[e[0]].push_back(e[1]);
            g[e[1]].push_back(e[0]);
        }
        int res = 0;
        for(int i=0; i<n; i++){
            if(vist[i]){
                continue;
            }
            vector<int> p;
            queue<int> q;
            
            q.push(i);
            while(!q.empty()){
                auto t = q.front();
                q.pop();
                if(vist[t]){
                    continue;
                }
                p.push_back(t);
                vist[t] = true;
                for(auto v : g[t]){
                    q.push(v);
                }
            }
            // 判断每一个节点的度是否满足要求
            int cnt = p.size()-1;
            bool flag = true;
            for(auto c : p){
                if(cnt != g[c].size()){
                    flag = false;
                }
            }
            if(flag) res++;
        }
        return res;
    }
};