leetcode周赛 (2020/06/21)

一直忘了更新
周赛挺难
先更前两题

1. 数组异或操作

leetcode周赛 (2020/06/21)_第1张图片

class Solution {
public:
    int xorOperation(int n, int start) {
        int ans = 0;
        for(int i=0;i<n;i++)
        {
            ans ^= start+2*i;
        }
        return ans;
    }
};

2. 保证文件名唯一

leetcode周赛 (2020/06/21)_第2张图片

class Solution {
public:
    vector<string> getFolderNames(vector<string>& names) {
        vector<string> ans;
        map<string, int> m;
        set<string> st;
        for(auto name: names)
        {
            if(st.find(name)==st.end())
            {
                st.insert(name);
                ans.push_back(name);
                m[name] = 1;
                //cout<
            }
            else{
                //cout<
                while(st.find(name+"("+to_string(m[name])+")")!=st.end())
                {
                    m[name]++;
                }
                string tmp = name+"("+to_string(m[name])+")";
                st.insert(tmp);
                ans.push_back(tmp);
                m[tmp] = 1;
                
            }
        }
        return ans;
    }
};

3. 避免洪水泛滥

leetcode周赛 (2020/06/21)_第3张图片

4. 找到最小生成树里的关键边和伪关键边

leetcode周赛 (2020/06/21)_第4张图片

最小生成树的题目练习得很少,比赛时看到很少人过,就没仔细看这个题了。其实这个题就是个最小生成树Kruskal算法,枚举每条边判断一下是否为关键边\伪关键边即可。

代码

class Solution {
vector<int> father;
int find(int x)
{
    if(father[x]!=x)
    {
        father[x] = find(father[x]);
    }
    return father[x];
}
int getCost1(int n, vector<vector<int>>&edges, int k)
{
    for(int i=0;i<n;i++)
        father[i] = i;
    int res = 0, cnt=n;
    for(auto &edge : edges)
    {
        int a = edge[0], b=edge[1], w=edge[2];
        if(edge[3] == k)
            continue;
        int fx = find(a);
        int fy = find(b);
        if(fx != fy)
        {
            father[fx] = fy;
            res += w;
            cnt--;
        }
        
    }
    if(cnt>1)
        res = INT_MAX;
    return res; 
}
int getCost2(int n,vector<vector<int>>&edges, int k)
{
    for(int i=0;i<n;i++)
        father[i] = i;
    int res = 0, cnt = n;
    for(auto &edge : edges)
    {
        if(edge[3]==k)
        {
            int a = edge[0], b = edge[1], w = edge[2];
            int fx = find(a);
            int fy = find(b);
            if(fx!=fy)
            {
                father[fx] = fy;
                res += w;
                cnt--;
            }
            break;
        }
    }
    for(auto &edge : edges)
    {
        int a = edge[0], b = edge[1], w = edge[2];
        int fx = find(a);
        int fy = find(b);
        if(fx!=fy){
            father[fx] = fy;
            res += w;
            cnt--;
        }
    }
    if(cnt>1)
        res = INT_MAX;
    return res;
}
public:
    vector<vector<int>> findCriticalAndPseudoCriticalEdges(int n, vector<vector<int>>& edges) {
        int M = edges.size();
        for(int i=0;i<M;i++)
        {
            edges[i].push_back(i);
        }
        sort(edges.begin(),edges.end(),[](vector<int> A, vector<int> B){return A[2]<B[2];});
        father = vector<int>(n);
        
        int cost = getCost1(n, edges, -1);
        vector<vector<int>> ans = vector<vector<int>>(2);
        for(int k=0;k<M;k++)
        {
            if(getCost1(n,edges,k)!=cost)
                ans[0].push_back(k);
            else if(getCost2(n,edges,k)==cost)
                ans[1].push_back(k);
        }
        return ans;
    }
};

你可能感兴趣的:(leetcode,leetcode周赛)