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

文章目录

    • 1. 比赛结果
    • 2. 题目
      • 1. LeetCode 5420. 商品折扣后的最终价格 easy
      • 2. LeetCode 5422. 子矩形查询 medium
      • 3. LeetCode 5423. 找两个和为目标值且不重叠的子数组 medium
      • 4. LeetCode 5421. 安排邮筒 hard

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 最终需要支付的价格。

示例 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 。
商品 34 都没有折扣。示例 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

解题:

  • 读懂题目就可以了
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

class Solution:# py3def 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

  • 数据规模大的话,需要用单调栈
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) 的当前值。
示例 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

解题:

  • 暴力更新
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

  • 或者不用更新,直接逆序查历史记录
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

class SubrectangleQueries:# py3def __init__(self, rectangle: List[List[int]]):import numpy as npself.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] = newValuedef 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 。

示例 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,然后外层循环 注意剪枝
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
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;//前缀和,indexm[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;//前缀和,indexm1[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:
在这里插入图片描述

输入:houses = [1,4,8,10,20], k = 3
输出:5
解释:将邮筒分别安放在位置 3920 处。
每个房子到最近邮筒的距离和为 |3-1| + |4-3| + |9-8| + |10-9| + |20-20| = 5

示例 2:
在这里插入图片描述

输入:houses = [2,3,5,12,18], k = 2
输出:9
解释:将邮筒分别安放在位置 314 处。
每个房子到最近邮筒距离和为 |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 中的整数互不相同。

解题:

待补

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/475545.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

决策树模型 - (ID3算法、C4.5算法) - Python代码实现

目录 算法简介 信息熵(Entropy) 信息增益(Information gain) - ID3算法 信息增益率(gain ratio) - C4.5算法 源数据 代码实现 - ID3算法 代码实现 - C4.5算法 画决策树代码-treePlotter 算法简介 决策数(Decision Tree)在机器学习中也是比较常见的一种算法&#xff0c…

SGA介绍

以前一直看的马马虎虎&#xff0c;这次重新整理了下sga设置&#xff0c;组件等。当然这些涉及到了很多的参考&#xff0c;主要的参考的网址&#xff1a;http://www.hellodba.com/reader.php?ID104&langCNhttp://8xmax.blog.163.com/blog/static/1633631020084781125726/ h…

重复值处理 - 清洗 DataFrame 中的各种重复类型 - Python代码

目录 所有列是否完全重复 指定某一列是否重复 根据多列判断是否重复&#xff0c;防止误删数据 其他数据预处理方法 通过八爪鱼或者火车头等采集器从全网抓取的数据中&#xff0c;总会存在各种各样的重复数据&#xff0c;为保证数据在使用过程中的准确性&#xff0c;总要先进…

LeetCode 1480. 一维数组的动态和(前缀和)

1. 题目 给你一个数组 nums 。数组「动态和」的计算公式为&#xff1a;runningSum[i] sum(nums[0]…nums[i]) 。 请返回 nums 的动态和。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,3,4] 输出&#xff1a;[1,3,6,10] 解释&#xff1a;动态和计算过程为 [1, 12, 123, …

bitmap 转 drawable

BitmapDrawable drawable new BitmapDrawable(bitmap); layout.setBackgroundDrawable(drawable);转载于:https://www.cnblogs.com/sode/archive/2011/08/10/2133799.html

机器学习与建模 - 聚类、分类、回归的区别

一句话概括&#xff1a; 1. 聚类&#xff1a;无监督学习&#xff0c;学习结果将产生几个集合&#xff0c;集合中的元素彼此相似&#xff1b; 2. 分类&#xff1a;有监督学习&#xff0c;学习结果将产生几个函数&#xff0c;通过函数划分为几个集合&#xff0c;数据对象是离散…

LeetCode 1481. 不同整数的最少数目(计数+排序+贪心)

1. 题目 给你一个整数数组 arr 和一个整数 k 。现需要从数组中恰好移除 k 个元素&#xff0c;请找出移除后数组中不同整数的最少数目。 示例 1&#xff1a; 输入&#xff1a;arr [5,5,4], k 1 输出&#xff1a;1 解释&#xff1a;移除 1 个 4 &#xff0c;数组中只剩下 5 一…

Silverlight带关闭动画的内容控件,可移动的内容控件(一)

本例给大家介绍两个自定义控件&#xff0c;一个有显示和关闭两种状态&#xff0c;在状态切换时有动画效果。另外一个是可以拖动的内容控件&#xff0c;可以制作能拖动的面板。 A&#xff0e;带关闭动画的内容控件。 .xaml View Code <ResourceDictionary xmlns"htt…

模型评价 - 判断数据模型拟合效果的三种方法

数据建模的目的就是获得从自变量映射到因变量的函数&#xff0c;在建模的探索过程中&#xff0c;不同的方式总会得出不同的函数模型&#xff0c;而这些函数大多是由一些参数构成的&#xff0c;比如 y f&#xff08; x; w0, w1, w2, w3, ...&#xff09;。 平方损失函数 为了选…

Autodesk云计算系列视频 --- 云计算与Civil 3D

前面的视频介绍了云计算与AutoCAD/Revit/Inventor的结合&#xff0c;这一节是云计算与Civil 3D的结合例子&#xff1a; 演示中使用的云计算程序源代码可以从下面链接下载&#xff1a; The sample code used in the demonstration is available here. 转载于:https://www.cnblo…

模型评价 - 机器学习与建模中怎么克服过拟合问题?

上一篇博客链接&#xff1a; 机器学习与建模中 - 判断数据模型拟合效果的三种方法 在上一篇博客中&#xff0c;我们谈到了使用损失函数来判断模型的拟合效果。但是拟合效果比较好的模型不一定是最好的模型&#xff0c;建模的最终目的是为了预测&#xff0c;因此预测最精准的模…

LeetCode 957. N 天后的牢房(查找循环节)

1. 题目 8 间牢房排成一排&#xff0c;每间牢房不是有人住就是空着。 每天&#xff0c;无论牢房是被占用或空置&#xff0c;都会根据以下规则进行更改&#xff1a; 如果一间牢房的两个相邻的房间都被占用或都是空的&#xff0c;那么该牢房就会被占用。 否则&#xff0c;它就…

获取数据 - 下载附件解压附件 - Python代码

一些线上化刚刚起步的部门&#xff0c;并不是所有的数据都是直接推送到服务器的数据库中&#xff0c;有些数据往往是数据中心通过邮件形式推送的&#xff0c;如果每天接收邮件--下载附件--解压--合并文件--导入数据库&#xff0c;对于数据工程师来说&#xff0c;这无疑是琐碎且…

技术标书的写法

1, 背景&#xff0c;用户对什么关心&#xff0c;就说什么。即使没有软件也可以&#xff0c;用画图软件先画出来。2&#xff0c;用户招标流程&#xff0c;弄到评分标准一切就OK 了&#xff0c;比如说什么时候该上台演示&#xff0c;如果没有评分标 准&#xff0c;站在评审角度…

LeetCode 947. 移除最多的同行或同列石头(并查集)

1. 题目 我们将石头放置在二维平面中的一些整数坐标点上。每个坐标点上最多只能有一块石头。 每次 move 操作都会移除一块所在行或者列上有其他石头存在的石头。 请你设计一个算法&#xff0c;计算最多能执行多少次 move 操作&#xff1f; 示例 1&#xff1a; 输入&#xf…

因子分析模型

主成分分析和因子分析 #包载入 library(corrplot) library(psych) library(GPArotation) library(nFactors) library(gplots) library(RColorBrewer)1234567 主成分分析 主成分分析&#xff08;PCA&#xff09;是对针对大量相关变量提取获得很少的一组不相关的变量&#xff…

网络机器人开发商

http://soft.pt42.com/blog_backup_index.htm转载于:https://www.cnblogs.com/carl2380/archive/2011/09/01/2162136.html

因子分析模型 - 案例按步骤详解 - (SPSS建模)

一、SPSS中的因子分析。 步骤: &#xff08;1&#xff09;定义变量&#xff1a;x1-财政用于农业的支出的比重,x2-第二、三产业从业人数占全社会从业人数的比重&#xff0c;x3-非农村人口比重&#xff0c;x4-乡村从业人员占农村人口的比重&#xff0c;x5-农业总产值占农林牧总…

MVC View 中 html 属性名与关键字冲突问题的分析与解决

在 MVC 的 View 中&#xff0c;允许使用 {} 来定义元素的属性。不过&#xff0c;HTML 中的 class 属性名与 C# 中的类 class 是冲突的&#xff0c;所以&#xff0c;在使用的时候&#xff0c;会发现不能使用 class 这个属性。解决的办法是在 class 前面加上一个 符号&#xff0…

神经网络 - 用单层感知器实现多个神经元的分类 - (Matlab建模)

训练样本矩阵&#xff1a; P [0.1 0.7 0.8 0.8 1.0 0.3 0.0 –0.3 –0.5 –1.5; 1.2 1.8 1.6 0.6 0.8 0.5 0.2 0.8 –1.5 –1.3]; 训练样本对应的分类&#xff1a; T [1 1 1 0 0 1 1 1 0 0 ;0 0 0 0 0 1 1 1 1 1]; 用MATLAB实现分类&…