当前位置:网站首页>6091. Divide the array so that the maximum difference is K
6091. Divide the array so that the maximum difference is K
2022-07-02 15:40:00 【Laver (nori)】
Ideas :
Prioritize , Then expand every possible interval , When the difference between the two ends of the interval exceeds Given range , Go directly to the next interval ;
class Solution {
public:
int partitionArray(vector<int>& nums, int k) {
int ans = 0;
// Sort the array first
sort(begin(nums), end(nums));
// The first element of the first subsequence
int start = nums[0];
// The default is a
ans = 1;
for(int i = 1; i < nums.size(); i++){
if(nums[i] - start <= k){
// If it is in the current sequence, skip
continue;
}
// Not in the current sequence , Then record the beginning element of a sequence
start = nums[i];
// Sequence number accumulation
ans++;
}
return ans;
}
};
边栏推荐
- 彻底弄懂浏览器强缓存和协商缓存
- 【Leetcode】167-两数之和II -输入有序数组
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- 2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
- . Net again! Happy 20th birthday
- 2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
- LeetCode刷题——两整数之和#371#Medium
- 高考分数线爬取
- 03.golang初步使用
- 【LeetCode】19-删除链表的倒数第N个结点
猜你喜欢
Evaluation of embedded rz/g2l processor core board and development board of Feiling
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
Bing. Com website
怎样从微信返回的json字符串中截取某个key的值?
Pytoch saves tensor to Mat file
17_ Redis_ Redis publish subscription
Basic knowledge of cryptography
Leetcode skimming - remove duplicate letters 316 medium
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
随机推荐
Download blender on Alibaba cloud image station
Cultural scores of summer college entrance examination
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
10_ Redis_ geospatial_ command
19_ Redis_ Manually configure the host after downtime
密码学基础知识
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
LeetCode刷题——递增的三元子序列#334#Medium
Semantic segmentation learning notes (1)
02. After containerization, you must face golang
College entrance examination score line climbing
【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
Redux——详解
夏季高考文化成绩一分一段表
6096. 咒语和药水的成功对数
Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
PTA 天梯赛习题集 L2-001 城市间紧急救援
彻底弄懂浏览器强缓存和协商缓存
LeetCode刷题——两整数之和#371#Medium
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568