当前位置:网站首页>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;
}
};
边栏推荐
- 做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
- 18_ Redis_ Redis master-slave replication & cluster building
- 13_Redis_事务
- Download blender on Alibaba cloud image station
- Case introduction and problem analysis of microservice
- 【LeetCode】977-有序数组的平方
- How to find a sense of career direction
- Leetcode skimming -- sum of two integers 371 medium
- (4) Flink's table API and SQL table schema
- Beijing rental data analysis
猜你喜欢
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
[leetcode] 1162 map analysis
Basic knowledge of cryptography
03.golang初步使用
Leetcode question brushing - parity linked list 328 medium
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
08_ strand
. Net again! Happy 20th birthday
怎样从微信返回的json字符串中截取某个key的值?
Bing. Com website
随机推荐
【LeetCode】977-有序數組的平方
SQL transaction
怎样从微信返回的json字符串中截取某个key的值?
LeetCode刷题——奇偶链表#328#Medium
JVM architecture, classloader, parental delegation mechanism
[leetcode] 1254 - count the number of closed Islands
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
Custom exception
[leetcode] 486 predict winners
4. Jctree related knowledge learning
MySQL calculate n-day retention rate
4. Data splitting of Flink real-time project
彻底弄懂浏览器强缓存和协商缓存
PTA ladder game exercise set l2-001 inter city emergency rescue
Oracle primary key auto increment
高考录取分数线爬虫
【LeetCode】19-删除链表的倒数第N个结点
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
NBA player analysis
Evaluation of embedded rz/g2l processor core board and development board of Feiling