Class Solution
java.lang.Object
g3501_3600.s3578_count_partitions_with_max_min_difference_at_most_k.Solution
3578 - Count Partitions With Max-Min Difference at Most K.
Medium
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 109 + 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 * 1041 <= nums[i] <= 1090 <= k <= 109
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
countPartitions
public int countPartitions(int[] nums, int k)
-