当前位置:网站首页>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刷题——验证二叉树的前序序列化#331#Medium
- 怎样从微信返回的json字符串中截取某个key的值?
- 2303. 计算应缴税款总额
- LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
- Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
- College entrance examination admission score line crawler
- 【LeetCode】344-反转字符串
- 【LeetCode】876-链表的中间结点
- LeetCode刷题——两整数之和#371#Medium
- Yolov5 code reproduction and server operation
猜你喜欢
15_ Redis_ Redis. Conf detailed explanation
SQL stored procedure
自定义异常
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
Bing. Site Internet
【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
6.12 critical moment of Unified Process Platform
Solve the problem of frequent interruption of mobaxterm remote connection
随机推荐
【LeetCode】1254-统计封闭岛屿的数量
6091. 划分数组使最大差为 K
02.面向容器化后,必须面对golang
【LeetCode】344-反转字符串
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
Build your own semantic segmentation platform deeplabv3+
夏季高考文化成绩一分一段表
How to avoid 7 common problems in mobile and network availability testing
【LeetCode】486-预测赢家
Solve the problem of frequent interruption of mobaxterm remote connection
14_ Redis_ Optimistic lock
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
[leetcode] 977 - carré du tableau ordonné
【Salesforce】如何确认你的Salesforce版本?
LeetCode刷题——验证二叉树的前序序列化#331#Medium
党史纪实主题公益数字文创产品正式上线
密码学基础知识
Pytorch 保存tensor到.mat文件
【LeetCode】695-岛屿的最大面积
[leetcode] 695 - maximum area of the island