• leetcode竞赛:85 场双周赛


    链接:https://leetcode.cn/contest/biweekly-contest-85/
    日期:2022年08月20日
    1.
    定长滑动窗口

    class Solution {
    public:
        int minimumRecolors(string blocks, int k) {
            int n = blocks.size(), cnt = 0, res = 1000;
            for (int i = 0, j = 0; i < n; i++) {
                while (j < n && j - i < k) {
                    if (blocks[j++] == 'W') cnt++;
                }
                if (j - i == k) res = min(res, cnt);
                if (blocks[i] == 'W') cnt--;
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    暴力模拟

    class Solution {
    public:
        int secondsToRemoveOccurrences(string s) {
            int res = 0, n = s.size();
            while (true) {
                int flag = 0;
                for (int i = 1; i < n; i++) {
                    if (s[i - 1] == '0' && s[i] == '1') {
                        swap(s[i-1], s[i]);
                        i++;
                        flag = 1;
                    }
                }
    
                if (!flag) break;
                res++;
    
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    差分算法

    class Solution {
    public:
        string shiftingLetters(string s, vector<vector<int>>& shifts) {
            int n = s.size();
            vector<int> t(n + 1);
            for (auto &p:shifts) {
                int a = p[0], b = p[1], d = p[2];
                if (d == 0) d = -1;
                t[a] += d;
                t[b + 1] -= d;
            }
    
            for (int i = 1; i <= n; i++) {
                t[i] += t[i - 1];
                t[i - 1] = t[i - 1] % 26 + 26;
                char c = (s[i - 1] - 'a' + t[i - 1]) % 26 + 'a';
                s[i - 1] = c;
            }
            return s;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    逆向并查集 做法

    class Solution {
    public:
        typedef long long LL;
        vector<LL> s;
        vector<int> p;
        int find(int x) {
            if (p[x] != x) {
                p[x] = find(p[x]);
            }
            return p[x];
        }
        LL Union(int a, int b) {
            a = find(a), b = find(b);
            p[a] = b;
            s[b] += s[a];
            return s[b];
        }
        vector<long long> maximumSegmentSum(vector<int>& nums, vector<int>& removeQueries) {
            vector<LL> res;
            int n = removeQueries.size();
            for (int i = 0; i < nums.size(); i++) {
                p.push_back(i);
                s.push_back(0);
            }
            LL mx = 0;
            for (int i = n - 1; i >= 0; i--) {
                res.push_back(mx);
                int x = removeQueries[i];
                s[x] = nums[x];
                if (x > 0 && s[x - 1]) mx = max(mx, Union(x, x - 1));
                if (x < n - 1 && s[x + 1]) mx = max(mx, Union(x, x + 1));
                mx = max(mx, s[x]);
            }
            reverse(res.begin(), res.end());
            return res;
        }
    };
    
    • 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

    模拟列项正向维护分裂数组的最大值:二分,lower_bound

    class Solution {
    public:
        typedef long long LL;
        vector<long long> maximumSegmentSum(vector<int>& nums, vector<int>& removeQueries) {
            int n = nums.size();
            vector<LL> f(n + 1), res;
            for (int i = 0; i < n; i++) {
                f[i + 1] = f[i] + nums[i];
            }
            set<int> st; 
            st.insert(0); st.insert(n);
            multiset<long long> ms;
            ms.insert(f[n]);
            for (int pos : removeQueries) {
                auto i = st.upper_bound(pos);
                auto j = (i--);
                int L = *i, R = *j;
                ms.erase(ms.lower_bound(f[R] - f[L]));
                st.insert(pos);
                st.insert(pos + 1);
                ms.insert(f[pos] - f[L]);
                ms.insert(f[R] - f[pos + 1]);
                res.push_back(*ms.rbegin());
            }
            
            return res;
        }
    };
    
    
    • 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
  • 相关阅读:
    ssm在线学习系统毕业设计-附源码211707
    使用 Sonar+Epona+Gitlab+dingding 搭建代码静态检查系统
    颠覆与创新:探寻Facebook未来的发展路径
    流血、止血、再造血,AI独角兽们何时涅槃?
    docker (六)-进阶篇-数据持久化最佳实践MySQL部署
    c++STL容器(看这一篇就够)
    flink 状态
    vue3 ElementUI Switch before-change自动调用问题
    【机器学习】实验3布置:贝叶斯垃圾邮件识别
    iOS swift5 提示信息显示,提示弹框,第三方框架XHToastSwift
  • 原文地址:https://blog.csdn.net/weixin_43233774/article/details/126448080