【LeetCode】第 379 场周赛题解

41 阅读1分钟

100170. 对角线最长的矩形的面积

class Solution {
public:
    int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {
        int res = 0, t = 0;
        for(auto c : dimensions){
            if(c[0]*c[0]+c[1]*c[1] >= t){
                if(t == c[0]*c[0]+c[1]*c[1]){
                    res = max(res, c[0]*c[1]);    
                }else{
                    t = c[0]*c[0]+c[1]*c[1];
                    res = c[0]*c[1];
                }
            }
        }
        return res;
    }
};

100187. 捕获黑皇后需要的最少移动次数

题解:分类讨论

  1. 车和皇后在水平和垂直线上,中间没有阻挡,只要 1 步,如果有阻挡,需要移开象,那么就要 2 步。
  2. 象和皇后在对角线上,中间没有阻挡,只要 1 步,如果有阻挡,需要移开车,那么就要2步。
class Solution {
public:
    // 判断不在两点中间
    bool check(int l, int m, int r) { 
        return m < min(l, r) || m > max(l, r); 
    }
    
    int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
        int res;
        // 判断车的情况
        if (a == e && (c != e || check(b, d, f))) {
            return 1;
        }
        if (b == f && (d != f || check(a, c, e))) {
            return 1;
        }
        // 判断象的情况
        if (c + d == e + f && (a + b != e + f || check(c, a, e))) {
            return 1;
        }
        if (c - d == e - f && (a - b != e - f || check(c, a, e))) {
            return 1;
        }
        return 2;
    }
};

100150. 移除后集合的最多元素数

class Solution {
public:
    int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {
        int n = nums1.size();
        unordered_set<int> s1(nums1.begin(), nums1.end());
        unordered_set<int> s2(nums2.begin(), nums2.end());
        int c = 0;// 共有元素的个数
        for(auto v : s1){
            if(s2.count(v)){
                c++;
            }
        }
        // 需要移出元素的个数
        int diff = max((int)s1.size()-n/2, 0) + max((int)s2.size()-n/2, 0);
        // 不重复元素的个数
        int len = s1.size() + s2.size();
        return len - max(diff, c);
    }
};