第 123 场 LeetCode 双周赛题解

A 三角形类型 II

第 123 场 LeetCode 双周赛题解_第1张图片
第 123 场 LeetCode 双周赛题解_第2张图片

三条边能构成三角形的充要条件是任意一边都小于其余两边之和,枚举各边判断能否构成三角形,若能再判断是否存在边想等

class Solution {
public:
    string triangleType(vector<int> &nums) {
        int s = accumulate(nums.begin(), nums.end(), 0);
        for (int i = 0; i < 3; i++)
            if (nums[i] >= s - nums[i])
                return "none";
        if (nums[0] == nums[1] && nums[1] == nums[2])
            return "equilateral";
        if (nums[0] == nums[1] || nums[1] == nums[2] || nums[0] == nums[2])
            return "isosceles";
        return "scalene";
    }

B 人员站位的方案数 I

第 123 场 LeetCode 双周赛题解_第3张图片
第 123 场 LeetCode 双周赛题解_第4张图片
第 123 场 LeetCode 双周赛题解_第5张图片
第 123 场 LeetCode 双周赛题解_第6张图片

枚举:枚举 liupengsay 和小羊肖恩的坐标,若两人的坐标满足 liupengsay 在左上小羊肖恩在右下,再枚举判断是否存在坐标在以两人为对角线形成的矩阵中,若存在则当前坐标对为可行的方案

class Solution {
public:
    int numberOfPairs(vector<vector<int>> &points) {
        int n = points.size();
        auto cmp = [&](vector<int> &lu, vector<int> &rd) {//判读是否满足lu在左上,rd在右下
            return lu[0] <= rd[0] && lu[1] >= rd[1];
        };
        int res = 0;
        for (int i = 0; i < n; i++)//枚举liupengsay的位置
            for (int j = 0; j < n; j++)//枚举小羊肖恩的位置
                if (i != j && cmp(points[i], points[j])) {//两人坐标满足条件
                    int tag = 1;
                    for (int k = 0; k < n; k++)//判断是否存在坐标在以两人为对角线形成的矩阵
                        if (k != i && k != j && cmp(points[i], points[k]) && cmp(points[k], points[j]))
                            tag = 0;
                    if (tag)
                        res++;
                }
        return res;
    }
};

C 最大好子数组和

第 123 场 LeetCode 双周赛题解_第7张图片第 123 场 LeetCode 双周赛题解_第8张图片

前缀和 + 哈希表:枚举 n u m s [ i ] nums[i] nums[i] ,用哈希表记录遍历过的末尾元素为 v v v 的最小前缀和: m n [ v ] = m i n ( { ∑ i = 0 k n u m s [ i ]    ∣    k < i , n u m s [ k ] = v } ) mn[v]=min(\{\sum_{i=0}^knums[i]\;|\; kmn[v]=min({i=0knums[i]k<i,nums[k]=v}),若之前遇到过元素为 n u m s [ i ] + k nums[i]+k nums[i]+k n u m s [ i ] − k nums[i]-k nums[i]k 则尝试更新答案

class Solution {
public:
    using ll = long long;
    long long maximumSubarraySum(vector<int> &nums, int k) {
        ll res = INT64_MIN;
        unordered_map<int, ll> mn;//哈希表
        ll ps = 0;//当前前缀和
        for (int i = 0; i < nums.size(); i++) {
            ps += nums[i];
            if (ll pre = nums[i] - k;mn.count(pre))//之前遇到过nums[i]-k
                res = max(res, ps - (mn[pre] - pre));
            if (ll pre = nums[i] + k;mn.count(pre))//之前遇到过nums[i]+k
                res = max(res, ps - (mn[pre] - pre));
            //更新哈希表    
            if (!mn.count(nums[i]))
                mn[nums[i]] = ps;
            else
                mn[nums[i]] = min(mn[nums[i]], ps);
        }
        return res != INT64_MIN ? res : 0;
    }
};

D 人员站位的方案数 II

第 123 场 LeetCode 双周赛题解_第9张图片
第 123 场 LeetCode 双周赛题解_第10张图片
第 123 场 LeetCode 双周赛题解_第11张图片
第 123 场 LeetCode 双周赛题解_第12张图片

哈希表 + 枚举 + 二分: 先遍历 p o i n t s points points 中的坐标 ( x , y ) (x,y) (x,y) ,将 x x x 加入数组 r o w [ y ] row[y] row[y]。然后将各个行数组 r o w [ i ] row[i] row[i] 排序,设 r o w row row 中各行数组按纵坐标降序排序,枚举 p o i n t s points points 中的坐标 ( x , y ) (x,y) (x,y),从纵坐标为 y y y 开始枚举各行,在对应行数组中二分查找不小于 x x x 的最小元素 u b ub ub,若存在且其小于 “之前行中能与 ( x , y ) (x,y) (x,y) 构成满足条件的坐标对的横坐标最小的 ( r i g h t , y i ) (right,yi) (right,yi) 的横坐标 r i g h t right right ”,则 ( x , y ) (x,y) (x,y) 能与 u b ub ub 构成构成满足条件的坐标对,同时更新 r i g h t right right

class Solution {
public:
    int numberOfPairs(vector<vector<int>> &points) {
        map<int, vector<int>, greater<int>> row;//哈希表按纵坐标记录行数组,各行按纵坐标降序排序
        for (auto &p: points)
            row[p[1]].push_back(p[0]);
        for (auto &[ci, ri]: row)//行数组排序
            sort(ri.begin(), ri.end());
        int res = 0;
        for (auto r = row.begin(); r != row.end(); r++) {//遍历各行
            for (auto it: r->second) {//遍历当前行中元素it
                int right;
                if (auto ub = upper_bound(r->second.begin(), r->second.end(), it);ub != r->second.end()) {//与it同一行,需要>it
                    res++;
                    right = *ub;//更新right
                } else
                    right = INT32_MAX;
                for (auto nr = next(r); nr != row.end(); nr++) {
                    if (auto ub = lower_bound(nr->second.begin(), nr->second.end(), it);ub != nr->second.end() && *ub < right) {与it非同一行,需要>=it
                        res++;
                        right = *ub;//更新right
                    } 
                }
            }
        }
        return res;
    }
};

你可能感兴趣的:(LeetCode,leetcode,算法,枚举,前缀和,哈希表,二分,有序集合)