Count the Number of Good Partitions - Problem
You are given a 0-indexed array nums consisting of positive integers.
A partition of an array into one or more contiguous subarrays is called good if no two subarrays contain the same number.
Return the total number of good partitions of nums. Since the answer may be large, return it modulo 109 + 7.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3,4]
›
Output:
8
💡 Note:
All elements are unique, so we can partition after any position. With 3 possible split points, we have 2³ = 8 ways: [1,2,3,4], [1][2,3,4], [1,2][3,4], [1,2,3][4], [1][2][3,4], [1][2,3][4], [1,2][3][4], [1][2][3][4]
Example 2 — Repeated Elements
$
Input:
nums = [1,2,3,2]
›
Output:
2
💡 Note:
Element 2 appears at positions 1 and 3, so we cannot split between them. We can only split after position 0. This gives us 2^1 = 2 valid partitions: [1,2,3,2] and [1][2,3,2]
Example 3 — All Same Elements
$
Input:
nums = [1,1,1,1]
›
Output:
1
💡 Note:
Since all elements are the same, we cannot split anywhere. Only one partition possible: [1,1,1,1]
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code