Leetcode 第 123 场双周赛题解

Leetcode 第 123 场双周赛题解

题目1:3024. 三角形类型 II

思路

分类讨论。

设三角形的三条边为 a,b,c,其中 a<b<c:

  1. a + b <= c,返回 “none”;
  2. 否则:
    • a==b==c,返回 “equilateral”;
    • a == b || b == c,返回 “isosceles”;
    • 三边互不相等,返回 “scalene”。

代码

/*
 * @lc app=leetcode.cn id=3024 lang=cpp
 *
 * [3024] 三角形类型 II
 */

// @lc code=start
class Solution
{
public:
    string triangleType(vector<int> &nums)
    {
        sort(nums.begin(), nums.end());
        int a = nums[0], b = nums[1], c = nums[2];
        if (a + b <= c)
            return "none";
        if (a == b && b == c)
            return "equilateral";
        else if (a == b || b == c)
            return "isosceles";
        return "scalene";
    }
};
// @lc code=end

复杂度分析

时间复杂度:O(nlogn),其中 n 是数组 nums 的长度,n=3。

空间复杂度:O(1)。

题目2:3025. 人员站位的方案数 I

思路

暴力枚举 liupengsay 和小羊肖恩的位置,当 liupengsay 和小羊肖恩的位置满足要求(liupengsay 建立的围栏必须确保 liupengsay 的位置是矩形的左上角,小羊肖恩的位置是矩形的右下角)时,再次遍历数组 points,判断其余点是否会落在 liupengsay 建立的围栏内,若不会,计数器 count 自增 1。

最后返回 count。

代码

/*
 * @lc app=leetcode.cn id=3025 lang=cpp
 *
 * [3025] 人员站位的方案数 I
 */

// @lc code=start

// 暴力

class Solution
{
public:
    int numberOfPairs(vector<vector<int>> &points)
    {
        int n = points.size();
        int count = 0;
        for (int i = 0; i < n; i++)
        {
            int x1 = points[i][0], y1 = points[i][1];
            for (int j = 0; j < n; j++)
            {
                if (i == j)
                    continue;

                int x2 = points[j][0], y2 = points[j][1];
                if (x1 <= x2 && y1 >= y2) // 围栏合法
                {
                    bool ok = true;
                    for (int k = 0; k < n; k++)
                    {
                        if (k == i || k == j)
                            continue;
                            
                        int x = points[k][0], y = points[k][1];
                        if (x >= x1 && x <= x2 && y >= y2 && y <= y1)
                        {
                            ok = false;
                            break;
                        }
                    }
                    if (ok)
                        count++;
                }
            }
        }
        return count;
    }
};
// @lc code=end

复杂度分析

时间复杂度:O(n3),其中 n 是数组 points 的元素个数。

空间复杂度:O(1)。

题目3:3026. 最大好子数组和

思路

哈希表 hash = unordered_map<int, long long> 存储数组 nums 的元素 x 及其到 x 为止的最小前缀和 preSum。

遍历数组 nums,设当前元素为 x,前缀和为 sum:

  1. 在哈希表中寻找键 x + k,若找到,更新答案 ans = max(ans, sum + x - it->second);
  2. 在哈希表中寻找键 x - k,若找到,更新答案 ans = max(ans, sum + x - it->second);
  3. 更新 hash[x],保证里面存储的是最小前缀和;
  4. 更新前缀和 sum += x。

最后返回 ans。

代码

/*
 * @lc app=leetcode.cn id=3026 lang=cpp
 *
 * [3026] 最大好子数组和
 */

// @lc code=start
class Solution
{
public:
    long long maximumSubarraySum(vector<int> &nums, int k)
    {
        long long ans = LLONG_MIN, sum = 0;
        unordered_map<int, long long> hash;
        for (int &x : nums)
        {
            auto it = hash.find(x + k);
            if (it != hash.end())
                ans = max(ans, sum + x - it->second);

            it = hash.find(x - k);
            if (it != hash.end())
                ans = max(ans, sum + x - it->second);

            it = hash.find(x);
            if (it == hash.end() || sum < it->second)
                hash[x] = sum;
            sum += x;
        }
        return ans == LLONG_MIN ? 0 : ans;
    }
};
// @lc code=end

复杂度分析

时间复杂度:O(n),其中 n 是数组 nums 的元素个数。

空间复杂度:O(n),其中 n 是数组 nums 的元素个数。

题目4:3027. 人员站位的方案数 II

思路

第二题的数据加强版。

将 points 按照横坐标从小到大排序,横坐标相同的,按照纵坐标从大到小排序。

如此一来,在枚举 points[i] 和 points[j] 时(i<j),就只需要关心纵坐标的大小。

固定 points[i],然后枚举 points[j]:

  • 如果 points[j][1] 比之前枚举的点的纵坐标都大,那么矩形内没有其它点,符合要求,答案加 1。
  • 如果 points[j][1] 小于等于之前枚举的某个点的纵坐标,那么矩形内有其它点,不符合要求。

所以在枚举 points[j] 的同时,需要维护纵坐标的最大值 maxY。这也解释了为什么横坐标相同的,按照纵坐标从大到小排序。这保证了横坐标相同时,我们总是优先枚举更靠上的点,不会误把包含其它点的矩形也当作符合要求的矩形。

最后返回答案。

代码

/*
 * @lc app=leetcode.cn id=3027 lang=cpp
 *
 * [3027] 人员站位的方案数 II
 */

// @lc code=start

// 排序 + 枚举

class Solution
{
public:
    int numberOfPairs(vector<vector<int>> &points)
    {
        sort(points.begin(), points.end(), [](const auto &p, const auto &q)
             { return p[0] != q[0] ? p[0] < q[0] : p[1] > q[1]; });

        int n = points.size();
        int count = 0;

        for (int i = 0; i < n; i++)
        {
            int y0 = points[i][1];
            int max_y = INT_MIN;
            for (int j = i + 1; j < n; j++)
            {
                int y = points[j][1];
                if (y <= y0 && y > max_y)
                {
                    max_y = y;
                    count++;
                }
            }
        }

        return count;
    }
};
// @lc code=end

复杂度分析

时间复杂度:O(n2),其中 n 是数组 points 的元素个数。

空间复杂度:O(1)。

  • 37
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UestcXiye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值