3578. Count Partitions With Max-Min Difference at Most K
Difficulty: Medium
Topics: Array, Dynamic Programming, Queue, Sliding Window, Prefix Sum, Monotonic Queue, Weekly Contest 453
You are given an integer array nums and an integer k. Your task is to partition nums into one or more non-empty contiguous segments such that in each segment, the difference between its maximum and minimum elements is at most k.
Return the total number of ways to partition nums under this condition.
Since the answer may be too large, return it modulo 10⁹ + 7.
Example 1:
- Input: nums = [9,4,1,3,7], k = 4
- Output: 6
-
Explanation: There are 6 valid partitions where the difference between the maximum and minimum elements in each segment is at most
k = 4:[[9], [4], [1], [3], [7]][[9], [4], [1], [3, 7]][[9], [4], [1, 3], [7]][[9], [4, 1], [3], [7]][[9], [4, 1], [3, 7]][[9], [4, 1, 3], [7]]
Example 2:
- Input: nums = [3,3,4], k = 0
- Output: 2
-
Explanation: There are 2 valid partitions that satisfy the given conditions:
[[3], [3], [4]][[3, 3], [4]]
Constraints:
2 <= nums.length <= 5 * 10⁴1 <= nums[i] <= 10⁹0 <= k <= 10⁹
Hint:
- Use dynamic programming.
- Let
dp[idx]be the count of ways to partition the array with the last partition ending at indexidx. - Try using a sliding window; we can track the minimum and maximum in the window using deques.
Solution:
We need to count the number of ways to partition an array into contiguous segments where each segment's (max - min) ≤ k.
We need to count partitions where for each segment:
- max(segment) - min(segment) ≤ k
This is a dynamic programming problem where:
-
dp[i]= number of valid partitions ending at index i - The transition is:
dp[i] = sum(dp[j-1])for all j ≤ i where segment [j, i] is valid
Approach
We can solve this using:
- Dynamic Programming to count ways
- Sliding Window to find valid segments
- Two Monotonic Queues to track min and max in current window
- Prefix Sum to efficiently compute sums of dp values
Key Insight
For position i, we need to find the leftmost index left such that all segments ending at i starting from left to i are valid. Then:
-
dp[i] = sum(dp[left-1] to dp[i-1])(with base case dp[-1] = 1)
Let's implement this solution in PHP: 3578. Count Partitions With Max-Min Difference at Most K
<?php
/**
* @param Integer[] $nums
* @param Integer $k
* @return Integer
*/
function countPartitions($nums, $k) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo countPartitions([9,4,1,3,7], 4) . "\n"; // Output: 6
echo countPartitions([3,3,4], 0) . "\n"; // Output: 2
?>
Explanation:
-
Monotonic Deques:
-
minDequemaintains indices with increasing values (minimum at front) -
maxDequemaintains indices with decreasing values (maximum at front)
-
-
Sliding Window:
-
leftis the start of the current valid window - For each
right, we expand and maintain the deques - If
max - min > k, we incrementleftand remove outdated indices from deques
-
-
Dynamic Programming:
-
dp[i]= sum of alldp[j]wherej < iand segment[j+1, i]is valid - Using prefix sums allows O(1) computation of this sum
-
-
Base Case:
- An empty array has 1 valid partition (doing nothing)
- Represented as
dp[-1] = 1orprefixSum[0] = 1
Time Complexity: O(n)
- Each element is pushed and popped from the deques at most once
- We traverse the array once with the sliding window
Space Complexity: O(n)
- For dp array, prefix sum array, and the two deques
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)