前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 第 28 场双周赛(505/2144,前23.6%)

LeetCode 第 28 场双周赛(505/2144,前23.6%)

作者头像
Michael阿明
发布2020-07-13 15:49:20
3520
发布2020-07-13 15:49:20
举报

1. 比赛结果

全国排名: 505 / 2144,23.6%;全球排名: 1944 / 8571,22.7%

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. 题目

1. LeetCode 5420. 商品折扣后的最终价格 easy

题目链接 给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。

商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j] <= prices[i]最小下标 ,如果没有满足条件的 j ,你将没有任何折扣。

请你返回一个数组,数组中第 i 个元素是折扣后你购买商品 i 最终需要支付的价格。

代码语言:javascript
复制
示例 1:
输入:prices = [8,4,6,2,3]
输出:[4,2,4,2,3]
解释:
商品 0 的价格为 price[0]=8 ,你将得到 prices[1]=4 的折扣,所以最终价格为 8 - 4 = 4 。
商品 1 的价格为 price[1]=4 ,你将得到 prices[3]=2 的折扣,所以最终价格为 4 - 2 = 2 。
商品 2 的价格为 price[2]=6 ,你将得到 prices[3]=2 的折扣,所以最终价格为 6 - 2 = 4 。
商品 3 和 4 都没有折扣。

示例 2:
输入:prices = [1,2,3,4,5]
输出:[1,2,3,4,5]
解释:在这个例子中,所有商品都没有折扣。

示例 3:
输入:prices = [10,1,1,6]
输出:[9,0,1,6]
 
提示:
1 <= prices.length <= 500
1 <= prices[i] <= 10^3

解题:

  • 读懂题目就可以了
代码语言:javascript
复制
class Solution {//C++
public:
    vector<int> finalPrices(vector<int>& prices) {
        int i, j, n = prices.size();
        for(i = 0; i < n-1; i++)
        {
            for(j = i+1; j < n; j++)
            {
                if(prices[j] <= prices[i])
                {
                    prices[i] -= prices[j];
                    break;
                }
            }
        }
        return prices;
    }
};

4 ms 9.9 MB

代码语言:javascript
复制
class Solution:# py3
    def finalPrices(self, prices: List[int]) -> List[int]:
        n = len(prices)
        for i in range(n-1):
            for j in range(i+1,n):
                if prices[j] <= prices[i]:
                    prices[i] -= prices[j]
                    break;
        return prices

44 ms 13.7 MB

  • 数据规模大的话,需要用单调栈
代码语言:javascript
复制
class Solution {	//C++
public:
    vector<int> finalPrices(vector<int>& prices) {
        int i, n = prices.size();
        stack<int> stk;
        vector<int> ans(prices);
        for(i = n-1; i >= 0; --i)
        {
            while(!stk.empty() && prices[i] < prices[stk.top()])
                stk.pop();
            if(!stk.empty())
                ans[i] -= prices[stk.top()];
            stk.push(i);
        }
        return ans;
    }
};

2. LeetCode 5422. 子矩形查询 medium

题目链接 请你实现一个类 SubrectangleQueries ,它的构造函数的参数是一个 rows x cols 的矩形(这里用整数矩阵表示),并支持以下两种操作:

  • updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) 用 newValue 更新以 (row1,col1) 为左上角且以 (row2,col2) 为右下角的子矩形。
  • getValue(int row, int col) 返回矩形中坐标 (row,col) 的当前值。
代码语言:javascript
复制
示例 1:
输入:
["SubrectangleQueries","getValue","updateSubrectangle",
"getValue","getValue","updateSubrectangle","getValue","getValue"]
[[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]
输出:
[null,1,null,5,5,null,10,5]
解释:
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]);  
// 初始的 (4x3) 矩形如下:
// 1 2 1
// 4 3 4
// 3 2 1
// 1 1 1
subrectangleQueries.getValue(0, 2); // 返回 1
subrectangleQueries.updateSubrectangle(0, 0, 3, 2, 5);
// 此次更新后矩形变为:
// 5 5 5
// 5 5 5
// 5 5 5
// 5 5 5 
subrectangleQueries.getValue(0, 2); // 返回 5
subrectangleQueries.getValue(3, 1); // 返回 5
subrectangleQueries.updateSubrectangle(3, 0, 3, 2, 10);
// 此次更新后矩形变为:
// 5   5   5
// 5   5   5
// 5   5   5
// 10  10  10 
subrectangleQueries.getValue(3, 1); // 返回 10
subrectangleQueries.getValue(0, 2); // 返回 5

示例 2:
输入:
["SubrectangleQueries","getValue","updateSubrectangle",
"getValue","getValue","updateSubrectangle","getValue"]
[[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]]
输出:
[null,1,null,100,100,null,20]
解释:
SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,1,1],[2,2,2],[3,3,3]]);
subrectangleQueries.getValue(0, 0); // 返回 1
subrectangleQueries.updateSubrectangle(0, 0, 2, 2, 100);
subrectangleQueries.getValue(0, 0); // 返回 100
subrectangleQueries.getValue(2, 2); // 返回 100
subrectangleQueries.updateSubrectangle(1, 1, 2, 2, 20);
subrectangleQueries.getValue(2, 2); // 返回 20
 
提示:
最多有 500 次updateSubrectangle 和 getValue 操作。
1 <= rows, cols <= 100
rows == rectangle.length
cols == rectangle[i].length
0 <= row1 <= row2 < rows
0 <= col1 <= col2 < cols
1 <= newValue, rectangle[i][j] <= 10^9
0 <= row < rows
0 <= col < cols

解题:

  • 暴力更新
代码语言:javascript
复制
class SubrectangleQueries {//C++
    vector<vector<int>> v;
public:
    SubrectangleQueries(vector<vector<int>>& rectangle) {
        v = rectangle;
    }
    
    void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
        int i,j;
        for(i = row1; i <= row2; ++i)
            for(j = col1; j <= col2; ++j)
                v[i][j] = newValue;
    }
    
    int getValue(int row, int col) {
        return v[row][col];
    }
};

84 ms 18.6 MB

  • 或者不用更新,直接逆序查历史记录
代码语言:javascript
复制
class SubrectangleQueries {
    vector<vector<int>> record;
    vector<vector<int>> v;
public:
    SubrectangleQueries(vector<vector<int>>& rectangle) {
        v = rectangle;
    }
    
    void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
        record.push_back({row1,col1,row2,col2,newValue});
    }
    
    int getValue(int row, int col) {
        for(int i = record.size()-1; i >= 0; --i)
        {
            if(row>=record[i][0] && row<=record[i][2] && col>=record[i][1] && col<=record[i][3])
                return record[i][4];
        }
        return v[row][col];
    }
};

84 ms 19.2 MB

代码语言:javascript
复制
class SubrectangleQueries:# py3

    def __init__(self, rectangle: List[List[int]]):
        import numpy as np
        self.rec = np.array(rectangle)

    def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:
        self.rec[row1:row2+1, col1:col2+1] = newValue

    def getValue(self, row: int, col: int) -> int:
        return int(self.rec[row][col])

140 ms 30.2 MB

3. LeetCode 5423. 找两个和为目标值且不重叠的子数组 medium

题目链接 给你一个整数数组 arr 和一个整数值 target 。

请你在 arr 中找 两个互不重叠的子数组 且它们的和都等于 target 。 可能会有多种方案,请你返回满足要求的两个子数组长度和最小值

请返回满足要求的最小长度和,如果无法找到这样的两个子数组,请返回 -1 。

代码语言:javascript
复制
示例 1:
输入:arr = [3,2,2,4,3], target = 3
输出:2
解释:只有两个子数组和为 3 ([3] 和 [3])。它们的长度和为 2 。

示例 2:
输入:arr = [7,3,4,7], target = 7
输出:2
解释:尽管我们有 3 个互不重叠的子数组和为 7 ([7], [3,4] 和 [7]),
但我们会选择第一个和第三个子数组,因为它们的长度和 2 是最小值。

示例 3:
输入:arr = [4,3,2,6,2,3,4], target = 6
输出:-1
解释:我们只有一个和为 6 的子数组。

示例 4:
输入:arr = [5,5,4,4,5], target = 3
输出:-1
解释:我们无法找到和为 3 的子数组。

示例 5:
输入:arr = [3,1,1,1,5,1,2,1], target = 3
输出:3
解释:注意子数组 [1,2] 和 [2,1] 不能成为一个方案因为它们重叠了。
 
提示:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8

解题:

  • 先通过滑动窗口求出所有的区间,注意 使用multiset时,才能保存长度一样的
  • 然后在区间里双重循环,内层找到一个解的时候就 break,然后外层循环 注意剪枝
代码语言:javascript
复制
struct cmp
{
    bool operator()(const pair<int,int>& a, const pair<int,int>& b)const
    {
        return a.second-a.first < b.second-b.first;
        // 或者使用 set ,但是这里要加入 <= 号,但是这是个很不好的,
        // set就是去重的,你弄个相同的在里面,很让人迷惑
    }
};
class Solution {
public:
    int minSumOfLengths(vector<int>& arr, int target) {
        int i=0, j=0, n = arr.size(), sum = 0;
        int minlen = INT_MAX;
        multiset<pair<int,int>,cmp> v;
        for(;j < n; ++j)
        {
            sum += arr[j];
            if(sum==target)
                v.insert({i,j});
            while(sum > target)
            {
                sum -= arr[i++];
                if(sum==target)
                    v.insert({i,j});
            }
            
        }
        for(auto it1 = v.begin(); it1 != v.end(); ++it1)
        {
            if(2*(it1->second-it1->first+1) >= minlen)
                break;//记得优化,容易超时
            auto it2 = it1;
            for(it2++; it2 != v.end(); ++it2)
            {
                if(it1->second < it2->first || it1->first > it2->second)
                {
                   minlen = min(minlen, it1->second - it1->first+it2->second - it2->first+2);
                   break;//找到了一个解,break,后面不会有更优的
                }
            }
        }
        return minlen==INT_MAX?-1:minlen;
    }
};

516 ms 87.8 MB

  • 利用前缀和,分别记录每个位置左侧的最短长度,右侧的最短长度
  • 再遍历一次求解最短的 l+r
代码语言:javascript
复制
class Solution {
public:
    int minSumOfLengths(vector<int>& arr, int target) {
        int i, n = arr.size(), sum = 0, minlen = INT_MAX;
        unordered_map<int,int> m;//前缀和,index
        m[0] = -1;
        vector<int> left(n,0);
        vector<int> right(n,0);
        for(i = 0; i < n; ++i)
        {
            sum += arr[i];
            m[sum] = i;
            if(m.count(sum-target))
                minlen = min(minlen, i-m[sum-target]);
            left[i] = minlen;
        }
        unordered_map<int,int> m1;//前缀和,index
        m1[0] = n;
        sum = 0;
        minlen = INT_MAX;
        for(i = n-1; i >= 0; --i)
        {
            sum += arr[i];
            m1[sum] = i;
            if(m1.count(sum-target))
                minlen = min(minlen, m1[sum-target]-i);
            right[i] = minlen;
        }
        minlen = INT_MAX;
        for(i = 0; i < n-1; ++i)
            if(left[i]!=INT_MAX && right[i+1]!=INT_MAX)//左右都存在
                minlen = min(minlen, left[i]+right[i+1]);
        return minlen==INT_MAX?-1:minlen;
    }
};

1172 ms 164.8 MB

4. LeetCode 5421. 安排邮筒 hard

题目链接 给你一个房屋数组houses 和一个整数 k ,其中 houses[i] 是第 i 栋房子在一条街上的位置,现需要在这条街上安排 k 个邮筒。

请你返回每栋房子与离它最近的邮筒之间的距离的 最小 总和。

答案保证在 32 位有符号整数范围以内。

示例 1:

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
输入:houses = [1,4,8,10,20], k = 3
输出:5
解释:将邮筒分别安放在位置 3, 9 和 20 处。
每个房子到最近邮筒的距离和为 |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5 。

示例 2:

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
输入:houses = [2,3,5,12,18], k = 2
输出:9
解释:将邮筒分别安放在位置 3 和 14 处。
每个房子到最近邮筒距离和为 |2-3| + |3-3| + |5-3| + |12-14| + |18-14| = 9 。

示例 3:
输入:houses = [7,4,6,1], k = 1
输出:8

示例 4:
输入:houses = [3,6,14,10], k = 4
输出:0
 
提示:
n == houses.length
1 <= n <= 100
1 <= houses[i] <= 10^4
1 <= k <= n
数组 houses 中的整数互不相同。
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 比赛结果
  • 2. 题目
    • 1. LeetCode 5420. 商品折扣后的最终价格 easy
      • 2. LeetCode 5422. 子矩形查询 medium
        • 3. LeetCode 5423. 找两个和为目标值且不重叠的子数组 medium
          • 4. LeetCode 5421. 安排邮筒 hard
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档