↓Skip to main content
  1. LeetCode/

LeetCode 2963: Count the Number of Good Partitions

·2 mins· ·
LeetCode Hard Math Combinatorics Hash-Table Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-09-05

  • Submissions: Accepted ×2

Approach
#

“No two parts contain the same number” ⇔ every occurrence of a value lies in one part.

  1. Record each value’s last index last[x].
  2. Scan left to right, tracking how far the current block must reach, reach = max(last[x]). At i == reach the block ends and cannot be split further.
  3. That gives b indivisible blocks with b - 1 boundaries between them, each cut or not: 2^(b - 1), modulo.

This is the same idea as LeetCode 2584 and “Partition Labels”.

My Solution
#

class Solution:
    def numberOfGoodPartitions(self, nums: List[int]) -> int:
        last = {x: i for i, x in enumerate(nums)}  # last index of each value
        blocks = 0
        reach = 0
        for i, x in enumerate(nums):
            reach = max(reach, last[x])
            if i == reach:
                blocks += 1                        # an indivisible block ends
        return pow(2, blocks - 1, 10 ** 9 + 7)

My original skipped each whole block with a while loop, which is the same; one for loop tracking reach is more concise.

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • There are b - 1 cut points, not b.
  • Take the power of 2 modulo.

Interview Explanation
#

All occurrences of a value must be in one part, so I record each value’s last index and scan left to right; the current block must reach the last index of everything seen, and arriving there closes an indivisible block. The b - 1 boundaries between b blocks are each cut or not, so the answer is 2^(b-1). Time O(n).

Related

LeetCode 952: Largest Component Size by Common Factor
·2 mins
LeetCode Hard Math Number-Theory Union-Find Hash-Table Array
LeetCode 952 note: sharing a factor ⇔ sharing a prime factor; factor each number and union indices with the same prime factor using Union-Find, with no pairwise GCDs
LeetCode 3518: Smallest Palindromic Rearrangement II
·3 mins
LeetCode Hard Math Combinatorics Counting Hash-Table String
LeetCode 3518 note: a palindrome is determined by its left half, so this is the k-th permutation of a multiset; try characters position by position and use permutation counts, updated in O(1), to find k’s branch
LeetCode 992: Subarrays with K Different Integers
·2 mins
LeetCode Hard Sliding-Window Hash-Table Counting Array
LeetCode 992 note: “exactly k kinds” = “at most k” − “at most k - 1”, where each at-most window adds right - left + 1 per right end
LeetCode 1627: Graph Connectivity With Threshold
·2 mins
LeetCode Hard Math Number-Theory Union-Find Array
LeetCode 1627 note: no pairwise GCDs; for each z > threshold, connect all multiples of z, O(n log n) unions in total
LeetCode 2912: Number of Ways to Reach Destination in the Grid
·3 mins
LeetCode Hard Dynamic-Programming State-Machine Math Combinatorics
LeetCode 2912 note: many cells, but only four classes relative to the destination (itself, same row, same column, neither); each class’s total moves together in O(k)
LeetCode 3873: Maximum Points Activated with One Addition
·2 mins
LeetCode Hard Union-Find Hash-Table Array
LeetCode 3873 note: merge points sharing an x or y into components; a new point joins at most two of them, so the answer is the two largest plus 1