【Leetcode】第 386 场周赛—题解

3046. Split the Array

You are given an integer array nums of even length. You have to split the array into two parts nums1 and nums2 such that:

  • nums1.length == nums2.length == nums.length / 2.
  • nums1 should contain distinct elements.
  • nums2 should also contain distinct elements.

Return true if it is possible to split the array, and false otherwise.

Example 1:

Input: nums = [1,1,2,2,3,4]
Output: true
Explanation: One of thepossible ways to split nums is nums1 = [1,2,3] and nums2 = [1,2,4].

Example 2:

Input: nums = [1,1,1,1]
Output: false
Explanation: The only possible way to split nums is nums1 = [1,1] and nums2 = [1,1]. Both nums1 and nums2 do not contain distinct elements. Therefore, we return false.

Constraints:

  • 1 <= nums.length <= 100
  • nums.length % 2 == 0
  • 1 <= nums[i] <= 100

AC:

class Solution {
private:
    bool isUnique(vector<int>& nums) {
        for (int i = 1; i < nums.size(); ++i) {
            if (nums[i] == nums[i - 1]) {
                return false;
            }
        }
        return true;
    }
public:
    bool isPossibleToSplit(vector<int>& nums) {
        vector<int> nums1, nums2;
        sort(nums.begin(), nums.end());
        int len = nums.size();
        for (int i = 0, j = 1; j <= len - 1; i += 2, j += 2) {
            nums1.push_back(nums[i]);
            nums2.push_back(nums[j]);
        }
        if (isUnique(nums1) && isUnique(nums2)) {
            return true;
        } else {
            return false;
        }
    }
};

ac

纯粹是一点脑子都没用,装模做样地把一个数组按照题目意思拆成两个,然后判断每个数组是否有重复的。
看了灵神的题解,才想到只需要判断每个数出现次数是否超过2!

class Solution {
public:
    bool isPossibleToSplit(vector<int>& nums) {
        unordered_map<int, int> cnt;
        for (int x : nums) {
            if (++cnt[x] > 2) {
                return false;
            }
        }
        return true;
    }
};

ac2

3047. Find the Largest Area of Square Inside Two Rectangles

There exist n rectangles in a 2D plane. You are given two 0-indexed 2D integer arrays bottomLeft and topRight, both of size n x 2, where bottomLeft[i] and topRight[i] represent the bottom-left and top-right coordinates of the ith rectangle respectively.

You can select a region formed from the intersection of two of the given rectangles. You need to find the largest area of a square that can fit inside this region if you select the region optimally.

Return the largest possible area of a square, or 0 if there do not exist any intersecting regions between the rectangles.

Example 1:

e1

Input: bottomLeft = [[1,1],[2,2],[3,1]], topRight =[[3,3],[4,4],[6,6]]
Output: 1
Explanation: A square with side length 1 can fit inside either the intersecting region of rectangle 0 and rectangle 1, or the intersecting region of rectangle 1 and rectangle 2.
Hence the largest area is side * side which is 1 * 1 == 1. It can be shown that a square with a greater side length can not fit inside any intersecting region.

Example 2:

e2

Input: bottomLeft = [[1,1],[2,2],[1,2]], topRight = [[3,3],[4,4],[3,4]]
Output: 1
Explanation: A square with side length 1 can fit inside either the intersecting region of rectangle 0 and rectangle 1, the intersecting region of rectangle 1 and rectangle 2, or the intersection region of all 3 rectangles.
Hence the largest area is side * side which is 1 * 1 == 1. It can be shown that a square with a greater side length can not fit inside any intersecting region. Note that the region can be formed by the intersection of more than 2 rectangles.

Example 3:

e3

Input: bottomLeft = [[1,1],[3,3],[3,1]], topRight = [[2,2],[4,4],[4,2]]
Output: 0
Explanation: No pair of rectangles intersect, hence, we return 0.

Constraints:

  • n == bottomLeft.length == topRight.length
  • 2 <= n <= 103
  • bottomLeft[i].length == topRight[i].length == 2
  • 1 <= bottomLeft[i][0], bottomLeft[i][1] <= 107
  • 1 <= topRight[i][0], topRight[i][1] <= 107
  • bottomLeft[i][0] < topRight[i][0]
  • bottomLeft[i][1] < topRight[i][1]

AC:

class Solution {
public:
    long long largestSquareArea(vector<vector<int>>& bottomLeft,
                                vector<vector<int>>& topRight) {
        long long ans = 0;
        for (int i = 0; i < bottomLeft.size(); i++) {
            auto& b1 = bottomLeft[i];
            auto& t1 = topRight[i];
            for (int j = i + 1; j < bottomLeft.size(); j++) {
                auto& b2 = bottomLeft[j];
                auto& t2 = topRight[j];
                int height = min(t1[1], t2[1]) - max(b1[1], b2[1]);
                int width = min(t1[0], t2[0]) - max(b1[0], b2[0]);
                int size = min(height, width);
                if (size > 0) {
                    ans = max(ans, (long long)size * size);
                }
            }
        }
        return ans;
    }
};

这里给出灵神的思路:
枚举两个矩形

如果矩形有交集,那么交集一定是矩形。求出这个交集矩形的左下角和右上角。

  • 左下角横坐标:两个矩形左下角横坐标的最大值。
  • 左下角纵坐标:两个矩形左下角纵坐标的最大值。
  • 右上角横坐标:两个矩形右上角横坐标的最小值。
  • 右上角纵坐标:两个矩形右上角纵坐标的最小值。
    知道坐标就可以算出矩形的长和宽,取二者最小值作为正方形的边长。

如果矩形没有交集,那么长和宽是负数,在计算面积前判断。


  • 20
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值