当前位置:网站首页>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;
}
};
边栏推荐
- 终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
- For the problem that Folium map cannot be displayed, the temporary solution is as follows
- 6.12 critical moment of Unified Process Platform
- LeetCode刷题——去除重复字母#316#Medium
- 【LeetCode】977-有序数组的平方
- QML pop-up frame, customizable
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- Leetcode question brushing - parity linked list 328 medium
- [leetcode] 695 - maximum area of the island
- 做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
猜你喜欢
Pytorch 保存tensor到.mat文件
Solution of Queen n problem
Evaluation of embedded rz/g2l processor core board and development board of Feiling
. Solution to the problem of Chinese garbled code when net core reads files
10_Redis_geospatial_命令
LeetCode刷题——递增的三元子序列#334#Medium
LeetCode刷题——两整数之和#371#Medium
LeetCode刷题——去除重复字母#316#Medium
Redux - detailed explanation
【LeetCode】417-太平洋大西洋水流问题
随机推荐
10_Redis_geospatial_命令
士官类学校名录
Summary of the first three passes of sqli Labs
Leetcode skimming -- count the number of numbers with different numbers 357 medium
02. After containerization, you must face golang
15_ Redis_ Redis. Conf detailed explanation
MySQL -- Index Optimization -- order by
NBA player analysis
The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
YOLOV5 代码复现以及搭载服务器运行
[leetcode] 877 stone game
Real estate market trend outlook in 2022
【LeetCode】695-岛屿的最大面积
Leetcode skimming -- sum of two integers 371 medium
14_ Redis_ Optimistic lock
I made an istio workshop. This is the first introduction
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
Leetcode skimming - remove duplicate letters 316 medium
How does the computer set up speakers to play microphone sound
How to choose a third-party software testing organization for automated acceptance testing of mobile applications