Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 1050 <= nums[i] <= 1050 <= k <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[3,6,1,2,5] 2
[1,2,3] 1
[2,2,4,5] 0
longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit)maximum-beauty-of-an-array-after-applying-operation)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2294: Partition Array Such That Maximum Difference Is K
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;
}
}
// Accepted solution for LeetCode #2294: Partition Array Such That Maximum Difference Is K
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
}
# Accepted solution for LeetCode #2294: Partition Array Such That Maximum Difference Is K
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
// Accepted solution for LeetCode #2294: Partition Array Such That Maximum Difference Is K
impl Solution {
pub fn partition_array(mut nums: Vec<i32>, k: i32) -> i32 {
nums.sort();
let mut ans = 1;
let mut a = nums[0];
for &b in nums.iter() {
if b - a > k {
a = b;
ans += 1;
}
}
ans
}
}
// Accepted solution for LeetCode #2294: Partition Array Such That Maximum Difference Is K
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;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.