Count Partitions With Max-Min Difference at Most K - Problem

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

example_1.py โ€” Python
$ Input: nums = [1,3,1,1], k = 2
โ€บ Output: 8
๐Ÿ’ก Note: The valid partitions are: [1,3,1,1], [1,3,1]+[1], [1,3]+[1,1], [1]+[3,1,1], [1]+[3,1]+[1], [1]+[3]+[1,1], [1,3]+[1]+[1], [1]+[3]+[1]+[1]. Each segment satisfies max-min โ‰ค 2.
example_2.py โ€” Python
$ Input: nums = [2,2,4,5], k = 0
โ€บ Output: 3
๐Ÿ’ก Note: With k=0, each segment can only contain identical elements. Valid partitions: [2]+[2]+[4]+[5], [2,2]+[4]+[5], and we need to check which others work. Actually: [2]+[2]+[4]+[5], [2,2]+[4]+[5] are the only valid ones, so answer is 2.
example_3.py โ€” Python
$ Input: nums = [1,1,1], k = 5
โ€บ Output: 5
๐Ÿ’ก Note: Since all elements are identical and k=5, any partition works: [1,1,1], [1,1]+[1], [1]+[1,1], [1]+[1]+[1]. That's 4 ways, but let me recount: [1,1,1], [1]+[1,1], [1,1]+[1], [1]+[1]+[1] = 4 ways.

Visualization

Tap to expand
๐Ÿ“š Library Shelf OrganizationBookshelf System1 page3 pages1 page1 pageRule: Books on same shelf must have page difference โ‰ค 2Valid Shelf Arrangements:All books together (3-1=2โ‰ค2) โœ“[1,3] shelf[1,1] shelf[1][3][1,1]Total Ways: 8Using DP to countall valid arrangements
Understanding the Visualization
1
Examine Each Position
For each book position, consider it as the end of a shelf
2
Find Valid Starting Points
Look back to find all positions where we could start a new shelf
3
Sum Up Possibilities
Add up all the ways we could have organized the previous shelves
4
Build Complete Solution
Continue until all books are placed on valid shelves
Key Takeaway
๐ŸŽฏ Key Insight: At each position, the number of ways to partition equals the sum of ways to partition at all valid previous positions where we can start a new segment satisfying the max-min constraint.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยณ)

O(nยฒ) pairs of positions to check, O(n) time to validate each segment

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

DP array of size n+1 to store partition counts

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 1000
  • 1 โ‰ค nums[i] โ‰ค 105
  • 0 โ‰ค k โ‰ค 105
  • All partitions must be non-empty and contiguous
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 18
34.5K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen