You're given an integer array nums and an integer k. Your mission is to partition this array into one or more non-empty contiguous segments such that within each segment, the difference between the maximum and minimum elements is at most k.
Think of it like organizing books on shelves where each shelf can only hold books whose page counts don't differ by more than k pages. You need to count all the different ways you can arrange these "shelves" (partitions).
Goal: Return the total number of valid partitioning ways.
Note: Since the answer can be very large, return it modulo 10^9 + 7.
Example: For nums = [1,3,1,1] and k = 2, you could partition as [1,3,1,1] (max-min = 3-1 = 2 โค k) or [1,3] + [1,1] (both segments satisfy the condition), among other ways.
Input & Output
Visualization
Time & Space Complexity
O(nยฒ) pairs of positions to check, O(n) time to validate each segment
DP array of size n+1 to store partition counts
Constraints
- 1 โค nums.length โค 1000
- 1 โค nums[i] โค 105
- 0 โค k โค 105
- All partitions must be non-empty and contiguous