当前位置:网站首页>leetcode 划分数组使最大差为 K
leetcode 划分数组使最大差为 K
2022-06-10 19:49:00 【我很忙2010】
给你一个整数数组 nums 和一个整数 k 。你可以将 nums 划分成一个或多个 子序列 ,使 nums 中的每个元素都 恰好 出现在一个子序列中。
在满足每个子序列中最大值和最小值之间的差值最多为 k 的前提下,返回需要划分的 最少 子序列数目。
子序列 本质是一个序列,可以通过删除另一个序列中的某些元素(或者不删除)但不改变剩下元素的顺序得到。
示例 1:
输入:nums = [3,6,1,2,5], k = 2 输出:2 解释: 可以将 nums 划分为两个子序列 [3,1,2] 和 [6,5] 。 第一个子序列中最大值和最小值的差值是 3 - 1 = 2 。 第二个子序列中最大值和最小值的差值是 6 - 5 = 1 。 由于创建了两个子序列,返回 2 。可以证明需要划分的最少子序列数目就是 2 。
示例 2:
输入:nums = [1,2,3], k = 1 输出:2 解释: 可以将 nums 划分为两个子序列 [1,2] 和 [3] 。 第一个子序列中最大值和最小值的差值是 2 - 1 = 1 。 第二个子序列中最大值和最小值的差值是 3 - 3 = 0 。 由于创建了两个子序列,返回 2 。注意,另一种最优解法是将 nums 划分成子序列 [1] 和 [2,3] 。
示例 3:
输入:nums = [2,2,4,5], k = 0 输出:3 解释: 可以将 nums 划分为三个子序列 [2,2]、[4] 和 [5] 。 第一个子序列中最大值和最小值的差值是 2 - 2 = 0 。 第二个子序列中最大值和最小值的差值是 4 - 4 = 0 。 第三个子序列中最大值和最小值的差值是 5 - 5 = 0 。 由于创建了三个子序列,返回 3 。可以证明需要划分的最少子序列数目就是 3 。
提示:
1 <= nums.length <= 1050 <= nums[i] <= 1050 <= k <= 105
C++
class Solution {
public:
int partitionArray(vector<int>& nums, int k) {
sort(nums.begin(),nums.end());
int res=0;
int a=nums[0];
int b=nums[0];
int n=nums.size();
for(int i=1;i<n;i++) {
a=min(a,nums[i]);
b=max(b,nums[i]);
if(abs(a-b)>k) {
a=nums[i];
b=nums[i];
res++;
}
}
return res+1;
}
};边栏推荐
- Fs4060a is a 4.2v/3a charging IC
- Microsoft Word 教程,如何在 Word 中更改页面方向、为页面添加边框?
- MySQL Basics
- Can you still have a wonderful life if you are laid off at the age of 35?
- Is Zhongyan futures a regular platform in China? Is it safe to open an account? Want to open a futures account
- MySQL - common functions
- 自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日
- 解决idea超过5个相同包的时候自动变成*的问题
- How to realize face verification quickly and accurately?
- 暗黑破坏神不朽WIKI地址 暗黑破坏神不朽数据库地址分享
猜你喜欢

CVPR 2022 Tsinghua University proposed unsupervised domain generalization (UDG)

观点丨Play and Earn 会让加密游戏误入歧途

LeetCode:497. 非重叠矩形中的随机点————中等

pytorch深度学习——卷积操作以及代码示例

Analysis on rendering principle of mobile terminal

AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘

Stacked bar graph move the mouse into the tooltip to prompt that the filter is 0 element, so as to realize custom bubbles

自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日

How to use Diablo immortal database

Microsoft Word 教程「5」,如何在 Word 中更改页边距、创建新闻稿栏?
随机推荐
国庆期间给大家推荐一个可能会成为2019最佳的CRUD工具
中衍期货公司是国内的正规平台吗?开户安全吗?想开个期货账户
Portable FDW framework for Pb
牛客网:数组中出现次数超过一半的数字
【Educational Codeforces Round 120 (Rated for Div. 2)】C. Set or Decrease
P5723 [deep base 4. example 13] prime number pocket
8.4v dual lithium battery professional charging IC (fs4062a)
What are the conditions for opening an account for agricultural futures? How much is the service charge for opening an account now?
canvas 高级功能(中)
Hm3416h buck IC chip pwm/pfm controls DC-DC buck converter
Build a BPMN modeling Web Service
割舍绳子/整数分割
Node (express) implements interfaces such as adding, deleting, modifying, and paging
Canvas advanced functions (medium)
Stacked bar graph move the mouse into the tooltip to prompt that the filter is 0 element, so as to realize custom bubbles
Enhance the target method with surround notifications - extract notes
pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)
The most common habits from more than 200 English papers written by gradua
Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?
An old programmer of about 10 years said: simple crud function enters the era of codeless development 1. Adding, deleting, modifying and checking interface information