当前位置:网站首页>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;
}
};边栏推荐
- Wechat Alipay account system and payment interface business process
- Download blender on Alibaba cloud image station
- LeetCode刷题——去除重复字母#316#Medium
- 6.12 critical moment of Unified Process Platform
- 4. Data splitting of Flink real-time project
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- Real estate market trend outlook in 2022
- 语义分割学习笔记(一)
- [leetcode] 417 - Pacific Atlantic current problem
- Data analysis thinking analysis methods and business knowledge - business indicators
猜你喜欢

JVM architecture, classloader, parental delegation mechanism

SQL stored procedure

Loss function and positive and negative sample allocation: Yolo series

How to intercept the value of a key from the JSON string returned by wechat?

FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA

Bing. Site Internet

MySQL calculate n-day retention rate

. Solution to the problem of Chinese garbled code when net core reads files

Leetcode skimming -- incremental ternary subsequence 334 medium

Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
随机推荐
Download blender on Alibaba cloud image station
Oracle primary key auto increment
[leetcode] 417 - Pacific Atlantic current problem
[network security] network asset collection
LeetCode刷题——奇偶链表#328#Medium
Leetcode skimming -- count the number of numbers with different numbers 357 medium
Leetcode skimming - remove duplicate letters 316 medium
[leetcode] 189 rotation array
Steps for Navicat to create a new database
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
I made an istio workshop. This is the first introduction
Solution of Queen n problem
6092. 替换数组中的元素
PTA 天梯赛习题集 L2-001 城市间紧急救援
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
08_ strand
【LeetCode】189-轮转数组
JVM architecture, classloader, parental delegation mechanism
Pytoch saves tensor to Mat file
. Net again! Happy 20th birthday