当前位置:网站首页>6091. 划分数组使最大差为 K
6091. 划分数组使最大差为 K
2022-07-02 12:10:00 【紫菜(Nori)】
思路:
先排序,再对每个可能的区间进行扩展,当区间两头差值超过 给定范围时,直接进入下一个区间;
class Solution {
public:
int partitionArray(vector<int>& nums, int k) {
int ans = 0;
// 先对数组排序
sort(begin(nums), end(nums));
// 第一个子序列的第一个元素
int start = nums[0];
// 默认记为一个
ans = 1;
for(int i = 1; i < nums.size(); i++){
if(nums[i] - start <= k){
// 如果在当前序列内则直接跳过
continue;
}
// 不在当前序列,则记录下一个序列的开头元素
start = nums[i];
// 序列数累加
ans++;
}
return ans;
}
};边栏推荐
- 15_ Redis_ Redis. Conf detailed explanation
- 2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
- 04.进入云原生后的企业级应用构建的一些思考
- Common English abbreviations for data analysis (I)
- LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
- LeetCode刷题——奇偶链表#328#Medium
- 【LeetCode】1140-石子游戏II
- [solution] educational codeforces round 82
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- Oracle primary key auto increment
猜你喜欢
随机推荐
SQL stored procedure
[leetcode] 200 number of islands
Case introduction and problem analysis of microservice
[leetcode] 977 - carré du tableau ordonné
【LeetCode】200-岛屿数量
Bing.com网站
List set & UML diagram
. Solution to the problem of Chinese garbled code when net core reads files
[leetcode] 486 predict winners
【LeetCode】1254-统计封闭岛屿的数量
08_ strand
提前批院校名称
【LeetCode】1162-地图分析
03.golang初步使用
MD5 encryption
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
MySQL calculate n-day retention rate
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
11_ Redis_ Hyperloglog_ command
04.进入云原生后的企业级应用构建的一些思考









