Class Solution
-
- All Implemented Interfaces:
public final class Solution3578 - Count Partitions With Max-Min Difference at Most K.
Medium
You are given an integer array
numsand an integerk. Your task is to partitionnumsinto one or more non-empty contiguous segments such that in each segment, the difference between its maximum and minimum elements is at mostk.Return the total number of ways to partition
numsunder this condition.Since the answer may be too large, return it modulo <code>10<sup>9</sup> + 7</code>.
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:
<code>2 <= nums.length <= 5 * 10<sup>4</sup></code>
<code>1 <= numsi<= 10<sup>9</sup></code>
<code>0 <= k <= 10<sup>9</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegercountPartitions(IntArray nums, Integer k)-
-
Method Detail
-
countPartitions
final Integer countPartitions(IntArray nums, Integer k)
-
-
-
-