• leetcode竞赛:20220918双周赛


    日期:20220917
    链接:https://leetcode.cn/contest/biweekly-contest-87/

    题目一

    日期问题:
    区间问题:求交集

    class Solution {
    public:
        int arr[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob) {
            for (int i = 1; i <= 12; i++) {
                arr[i] += arr[i - 1];
            }
            int a, b, c, d;
            process(arriveAlice, leaveAlice, a, b);
            process(arriveBob, leaveBob, c, d);
            // cout << a << " " << b << endl;
            // cout << c << " " << d << endl;
            if (b < c || d < a) return 0;
            if (a <= c) {
                return min(b, d) - c + 1;
            } else {
                 return min(b, d) - a + 1;
            }
            return 0;
            
        }
        
        void process(string& a, string& b, int& c, int& d) {
            int x = (a[0] - '0') * 10 + (a[1] - '0');
            int y = (a[3] - '0') * 10 + (a[4] - '0');
            c = arr[x - 1] + y;
            x = (b[0] - '0') * 10 + (b[1] - '0');
            y = (b[3] - '0') * 10 + (b[4] - '0');
            d = arr[x - 1] + y; 
        }
    };
    
    • 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

    第二次实现,还是用月份来硬算得。但是求交集比原来思路清晰一些了。但是没有想到转换成天数来求。如果用天数来求得话,会更简单。

    class Solution {
    public:
        int countDaysTogether(string a, string b, string c, string d) {
            int arr[20] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            vector aa = get(a), bb = get(b), cc = get(c), dd = get(d);
            if (aa > cc) {
                swap(aa, cc);
                swap(bb, dd);
            }
            if (bb < cc) return 0;
            int res = 0;
            if (bb > dd) {
                swap(bb, dd);
            }
            for (int i = cc[0]; i <= bb[0]; i++) {
                res += arr[i];
            }
            res -= (cc[1] - 1) + (arr[bb[0]] - bb[1]);
    
            return res;
        }
        vector get(string &s) {
            int a = (s[0] - '0') * 10 + s[1] - '0';
            int b = (s[3] - '0') * 10 + s[4] - '0';
            return {a, b};
        }
    };
    
    • 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

    题目二

    贪心算法
    匈牙利算法

    class Solution {
    public:
        int matchPlayersAndTrainers(vector& players, vector& trainers) {
            sort(players.begin(), players.end());
            sort(trainers.begin(), trainers.end());
            int n = players.size(), m = trainers.size();
            int res = 0;
            for (int i = 0, j = 0; i < n && j < m;) {
                int a = players[i], b = trainers[j];
                if (a <= b) {
                    res++;
                    i++, j++;
                    continue;
                } else {
                    j++;
                }
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    题目三

    class Solution {
    public:
        vector smallestSubarrays(vector& nums) {
            int n = nums.size();
            vector> x(32, vector(n + 1));
            nums.push_back(INT_MAX);
            vector ret;
            for (int i = n; i >= 0; i--) {
                int res = 1;
                for (int j = 0; j < 31; j++) {
                    if (i == n) {
                        x[j][i] = n;
                        continue;
                    }
                    if (nums[i] & (1 << j)) x[j][i] = i; 
                    else x[j][i] = x[j][i + 1];
                    if (x[j][i] == n) continue;
                    res = max(x[j][i] - i + 1, res);
                }
                ret.push_back(res);
            }
            reverse(ret.begin(), ret.end());
            ret.pop_back();
            return ret;
        }
    };
    
    • 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

    题目四

    没写出来.看答案是比较难想的一种枚举。对于任意一遍交易,对于其中的交易 i i i,必须满足等式
    m o n e y − ( a 0 − b 0 ) − ( a 1 − b 1 ) − ( a 2 − b 2 ) . . . . > = a i money - (a_0 - b_0) - (a_1 - b_1) - (a_2 - b_2) ....>= a_i money(a0b0)(a1b1)(a2b2)....>=ai(注意0, 1, 不是数组顺序,而是任意一种交易顺序)。整理一下有:
    m o n e y > = ( a 0 − b 0 ) + ( a 1 − b 1 ) + ( a 2 − b 2 ) . . . . + a i money>= (a_0 - b_0) + (a_1 - b_1) + (a_2 - b_2) .... + a_i money>=(a0b0)+(a1b1)+(a2b2)....+ai
    可以看到等式有两部分,前面的每笔交易结果之和与第 i i i笔交易需要的钱的和。这时候转换思维,想象 a i a_i ai不是第 i i i笔交易,而是对于交易 i i i,我的money最少需要准备多少钱,才能满足最坏情况的交易 i i i,那应该是右边最大。 a i a_i ai是固定的,那么只需要前面的和最大。前面和最大,要把所有 a > b a>b a>b的交易加起来,答案就呼之欲出了。
    贴一个学会之后自己写的代码。

    typedef long long LL;
    class Solution {
    public:
        long long minimumMoney(vector>& transactions) {
            LL sum = 0;
            for (auto &ii : transactions) {
                int a = ii[0], b = ii[1];
                if (a > b) sum += a - b;
            }
            LL res = 0;
            for (auto &ii : transactions) {
                int a = ii[0], b = ii[1];
                if (a > b) res = max(res, a + sum - a + b);
                else res = max(res, a + sum); 
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    R语言主成分分析可视化(颜值高,很详细)
    穷人版生产力工具,好用得飞起 「GitHub 热点速览」
    Aeraki 教程系列(八) | 如何将 Dubbo 服务接入到 Aeraki Mesh?
    二分查找:如何用最省内存的方式实现快速查找功能?
    实现strStr题解补充
    WPF 依赖属性原理、 附加属性
    刷题记录:牛客NC20276[SCOI2010]传送带
    IP代理识别API:预防欺诈和保护网络安全的必要工具
    Handler由浅入深的面试连环炮,你都掌握了哪些?
    Vue组件化编程(模块与组件、模块化与组件化,非单文件组件,单文件组件)
  • 原文地址:https://blog.csdn.net/weixin_43233774/article/details/126912891