LeetCode 题解工作台

划分数组使最大差为 K

给你一个整数数组 nums 和一个整数 k 。你可以将 nums 划分成一个或多个 子序列 ,使 nums 中的每个元素都 恰好 出现在一个子序列中。 在满足每个子序列中最大值和最小值之间的差值最多为 k 的前提下,返回需要划分的 最少 子序列数目。 子序列 本质是一个序列,可以通过删除另一个序列中…

category

3

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

中等 · 贪心·invariant

bolt

答案摘要

题目要求划分子序列,而不是子数组,因此子序列中的元素可以不连续。我们可以将数组 排序,假设当前子序列的第一个元素为 ,则子序列中的最大值和最小值的差值不会超过 。因此我们可以遍历数组 ,如果当前元素 与 的差值大于 ,则更新 为 ,并将子序列数目加 1。遍历结束后,即可得到最少子序列数目,注意初始时子序列数目为 。 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 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 <= 105
  • 0 <= nums[i] <= 105
  • 0 <= k <= 105
lightbulb

解题思路

方法一:贪心 + 排序

题目要求划分子序列,而不是子数组,因此子序列中的元素可以不连续。我们可以将数组 nums\textit{nums} 排序,假设当前子序列的第一个元素为 aa,则子序列中的最大值和最小值的差值不会超过 kk。因此我们可以遍历数组 nums\textit{nums},如果当前元素 bbaa 的差值大于 kk,则更新 aabb,并将子序列数目加 1。遍历结束后,即可得到最少子序列数目,注意初始时子序列数目为 11

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(logn)O(\log n)。其中 nn 为数组 nums\textit{nums} 的长度。

1
2
3
4
5
6
7
8
9
10
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
speed

复杂度分析

指标
时间O(n \log n)
空间O(S_n)
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate demonstrates an understanding of greedy algorithms and array partitioning.

  • question_mark

    The candidate focuses on maintaining an invariant for maximum and minimum values in each subsequence.

  • question_mark

    The candidate optimizes the solution for time and space complexity, considering sorting and linear traversal.

warning

常见陷阱

外企场景
  • error

    Overlooking the importance of sorting the array before partitioning.

  • error

    Incorrectly partitioning the array without ensuring that the max-min difference condition holds.

  • error

    Failing to consider the efficiency of the approach when working with large arrays.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if the array is already sorted? How would the solution change?

  • arrow_right_alt

    What if the constraint for the max-min difference is much smaller, or zero?

  • arrow_right_alt

    How would the solution be impacted if negative values were allowed in the array?

help

常见问题

外企场景

划分数组使最大差为 K题解:贪心·invariant | LeetCode #2294 中等