返回介绍

solution / 2200-2299 / 2294.Partition Array Such That Maximum Difference Is K / README_EN

发布于 2024-06-17 01:03:07 字数 4584 浏览 0 评论 0 收藏 0

2294. Partition Array Such That Maximum Difference Is K

中文文档

Description

You are given an integer array nums and an integer k. You may partition nums into one or more subsequences such that each element in nums appears in exactly one of the subsequences.

Return _the minimum number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is at most _k_._

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

 

Example 1:

Input: nums = [3,6,1,2,5], k = 2
Output: 2
Explanation:
We can partition nums into the two subsequences [3,1,2] and [6,5].
The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.
The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.
Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.

Example 2:

Input: nums = [1,2,3], k = 1
Output: 2
Explanation:
We can partition nums into the two subsequences [1,2] and [3].
The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.
The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.
Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].

Example 3:

Input: nums = [2,2,4,5], k = 0
Output: 3
Explanation:
We can partition nums into the three subsequences [2,2], [4], and [5].
The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.
The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.
The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.
Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 105
  • 0 <= k <= 105

Solutions

Solution 1

class Solution:
  def partitionArray(self, nums: List[int], k: int) -> int:
    nums.sort()
    ans, a = 1, nums[0]
    for b in nums:
      if b - a > k:
        a = b
        ans += 1
    return ans
class Solution {
  public int partitionArray(int[] nums, int k) {
    Arrays.sort(nums);
    int ans = 1, a = nums[0];
    for (int b : nums) {
      if (b - a > k) {
        a = b;
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int partitionArray(vector<int>& nums, int k) {
    sort(nums.begin(), nums.end());
    int ans = 1, a = nums[0];
    for (int& b : nums) {
      if (b - a > k) {
        a = b;
        ++ans;
      }
    }
    return ans;
  }
};
func partitionArray(nums []int, k int) int {
  sort.Ints(nums)
  ans, a := 1, nums[0]
  for _, b := range nums {
    if b-a > k {
      a = b
      ans++
    }
  }
  return ans
}
function partitionArray(nums: number[], k: number): number {
  nums.sort((a, b) => a - b);
  let ans = 1;
  let a = nums[0];
  for (const b of nums) {
    if (b - a > k) {
      a = b;
      ++ans;
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文