LeetCode-in-Java

2963. Count the Number of Good Partitions

Hard

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.

Example 1:

Input: nums = [1,2,3,4]

Output: 8

Explanation: The 8 possible good partitions are: ([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]), and ([1,2,3,4]).

Example 2:

Input: nums = [1,1,1,1]

Output: 1

Explanation: The only possible good partition is: ([1,1,1,1]).

Example 3:

Input: nums = [1,2,1,3]

Output: 2

Explanation: The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).

Constraints:

Solution

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int numberOfGoodPartitions(int[] nums) {
        Map<Integer, Integer> mp = new HashMap<>();
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            mp.put(nums[i], i);
        }
        int i = 0;
        int j = 0;
        int cnt = 0;
        while (i < n) {
            j = Math.max(j, mp.get(nums[i]));
            if (i == j) {
                cnt++;
            }
            i++;
        }
        int res = 1;
        for (int k = 1; k < cnt; k++) {
            res = res * 2;
            int mod = 1000000007;
            res %= mod;
        }
        return res;
    }
}