Number of Great Partitions - Problem
Imagine you're a team leader dividing your team into two groups for a competitive challenge. You need to ensure both groups are strong enough to handle their tasks effectively.
Given an array nums of positive integers and an integer k, you must partition the array into exactly two ordered groups such that:
- Each element belongs to exactly one group
- Both groups have a sum โฅ
k(making them "great" partitions) - The order of elements within each group matches their original order in the array
Goal: Count all possible distinct great partitions. Since the answer can be huge, return it modulo 109 + 7.
Example: With nums = [1,1,1,1] and k = 2, you could have group1=[1,1] and group2=[1,1], or group1=[1,1,1] and group2=[1], etc.
Input & Output
example_1.py โ Basic Case
$
Input:
nums = [1,1,1,1], k = 2
โบ
Output:
6
๐ก Note:
Valid partitions: ([1],[1,1,1]), ([1,1],[1,1]), ([1,1,1],[1]), ([1],[1,1,1]), ([1,1],[1,1]), ([1,1,1],[1]). Wait, we need to count distinct partitions based on positions, not values. Actually there are 6 valid ways to split.
example_2.py โ Impossible Case
$
Input:
nums = [1,2,3], k = 10
โบ
Output:
0
๐ก Note:
Total sum is 6, so it's impossible to have both groups with sum โฅ 10. No valid partitions exist.
example_3.py โ Edge Case
$
Input:
nums = [6,6], k = 6
โบ
Output:
2
๐ก Note:
Two valid partitions: ([6],[6]) and ([6],[6]). Since elements are at different positions, both ways count: group1=[nums[0]], group2=[nums[1]] and group1=[nums[1]], group2=[nums[0]].
Visualization
Tap to expand
Understanding the Visualization
1
Decision Point
At each contestant, decide which team they join
2
Track Progress
Keep track of current team compositions and skill totals
3
Validate & Count
Check if both teams meet requirements and count valid formations
Key Takeaway
๐ฏ Key Insight: Use dynamic programming to efficiently explore all possible team formations while avoiding redundant calculations through memoization. Track one team's skill total since the other is automatically determined.
Time & Space Complexity
Time Complexity
O(n ร S)
n positions ร S possible sums (where S is the total sum)
โ Linear Growth
Space Complexity
O(n ร S)
Memoization table size plus recursion stack
โก Linearithmic Space
Constraints
- 1 โค nums.length โค 1000
- 1 โค nums[i] โค 109
- 1 โค k โค 109
- Both groups must be non-empty
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code