LeetCode第193场周赛

LeetCode第193场周赛

https://leetcode-cn.com/contest/weekly-contest-193/

一维数组的动态和

class Solution {
public:
    vector<int> runningSum(vector<int>& nums) {
        vector<int> res;
        long long sum = 0;
        for(auto i : nums) 
            res.push_back(sum += i);
        return res;
    }
};

不同整数的最少数目

class Solution {
public:
    int findLeastNumOfUniqueInts(vector<int>& arr, int k) {
        typedef pair<int, int> P;
        priority_queue<P, vector<P>, greater<P> > que;
        unordered_map<int, int> cnt;
        for(auto i : arr) cnt[i] ++;
        for(auto i : cnt) 
            que.push(make_pair(i.second, i.first));
        
        while(k > 0) {
            pair<int, int> p = que.top();
            if(k - p.first >= 0) k -= p.first, que.pop();
            else break;
        }
        return que.size();
    }
};

制作 m 束花所需的最少天数

这里yxc 写的极好 @YXC 用区间来处理 常数大部分情况下更小

class Solution {
public:

    int get(int l, int r, int k) {
        return (r - l + 1) / k;
    }

    int minDays(vector<int>& bs, int m, int k) {
        vector<pair<int, int>> q;
        int n = bs.size();
        for (int i = 0; i < bs.size(); i ++ ) q.push_back({bs[i], i + 1});

        vector<int> l(n + 2), r(n + 2);
        sort(q.begin(), q.end());
        int sum = 0;
        for (auto x : q) {
            int i = x.second;
            if (l[i - 1] && r[i + 1]) {
                sum = sum - get(l[i - 1], i - 1, k) - get(i + 1, r[i + 1], k) + get(l[i - 1], r[i + 1], k);
                r[l[i - 1]] = r[i + 1];
                l[r[i + 1]] = l[i - 1];
            } else if (l[i - 1]) {
                sum = sum - get(l[i - 1], i - 1, k) + get(l[i - 1], i, k);
                r[l[i - 1]] = i;
                l[i] = l[i - 1];
            } else if (r[i + 1]) {
                sum = sum - get(i + 1, r[i + 1], k) + get(i, r[i + 1], k);
                r[i] = r[i + 1];
                l[r[i + 1]] = i;
            } else {
                sum = sum + get(i, i, k);
                r[i] = l[i] = i;
            }

            if (sum >= m) return x.first;
        }

        return -1;
    }
};

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/347323/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public:

    int chk(vector<int>& a, int day, int k) {
        int tp = 0, tmp = 0;
        for(auto i : a) {
            if(day >= i) {
                tmp ++;
                if(tmp == k) tmp = 0, tp ++;
            } else tmp = 0;
        }
        return tp;
    }

    int minDays(vector<int>& bloomDay, int m, int k) {
        int L = 0x3f3f3f3f, R = -1;
        for(auto i : bloomDay) L = min(L, i), R = max(R, i);
        R ++;
        int l = L, r = R;
        while(l < r) {
            int mid = l + r >> 1;
            if(chk(bloomDay, mid, k) >= m) r = mid;
            else l = mid + 1;
        }
        return l == R ? -1 : l;
    }
};

树节点的第 K 个祖先

书上倍增 以前只写DFS的 看到YXC写的BFS 翻了翻自己博客 依稀记得有BFS写法的

class TreeAncestor {
public:
    vector<vector<int>> dp;
    vector<vector<int>> G;

    TreeAncestor(int n, vector<int>& parent) {
        dp = vector<vector<int> >(n, vector<int>(32, -1));
        G = vector<vector<int> >(n);
        int root = 0;
        for(int i = 0; i < n; ++ i) {
            if(parent[i] == -1) root = i;
            else G[parent[i]].push_back(i);
        }
        queue<int> que; que.push(root);
        while(!que.empty()) {
            int fa = que.front(); que.pop();
            for(auto son : G[fa]) {
                dp[son][0] = fa;
                for(int i = 1; i <= 31; ++ i) {
                    if(dp[son][i - 1] != -1) 
                        dp[son][i] = dp[dp[son][i - 1]][i - 1];
                }
                que.push(son);
            }
        }
    }
    
    int getKthAncestor(int node, int k) {
        for(int i = 0; i <= 31; i ++) {
            if(k >> i & 1) node = dp[node][i];
            if(node == -1) break;
        }
        return node;
    }
};

/**
 * Your TreeAncestor object will be instantiated and called as such:
 * TreeAncestor* obj = new TreeAncestor(n, parent);
 * int param_1 = obj->getKthAncestor(node,k);
 */

你可能感兴趣的:(Leetcode)