Skip to main content

Dynamic Programming - Hard Level - Question 3

Dynamic Programming - Hard Level - Question 3


Leetcode 1955 Count Number of Special Subsequences

A sequence is special if it consists of a positive number of 0s, followed by a positive number of 1s, then a positive number of 2s.

For example, [0,1,2] and [0,0,1,1,1,2] are special.

In contrast, [2,1,0], [1], and [0,1,2,0] are not special.

Given an array nums (consisting of only integers 0, 1, and 2), return the number of different subsequences that are special. Since the answer may be very large, return it modulo 10^9 + 7.

A subsequence of an array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements. Two subsequences are different if the set of indices chosen are different.

Constraints:

1 <= nums.length <= 10^5

0 <= nums[i] <= 2


Analysis:

For each element, there are three different choices: 0, 1, or 2.

Let focus on the 0 element first. If one element is 0, then there are two kinds of subsequences ending with 0:

A. the subsequences ending with previous 0's;

B. the subsequences ending with the current 0.

If we define a variable, sum0, to record the number of A, then number of B is sum0 + 1. Why?

Because 

1. each of the previous subsequence can be combined with the current 0 to form a new subsequence, which gives the number of sum0 in total;

2. the current 0 itself can form a new subsequence, which gives 1.

When the current 0 becomes one of the previous 0's, we need to update sum0 = sum0 + (sum0 + 1), to make it include both A and B.

Similarly, we can obtain the following formula for the numbers of subsequences ending with 1 and 2.

sum1 = sum1 + (sum1 + sum0)

sum2 = sum2 + (sum2 + sum1)


See the code below:


class Solution {
public:
    int countSpecialSubsequences(vector<int>& nums) {
        long mod = 1e9+7, sum0 = 0, sum1 = 0, sum2 = 0;
        int n = nums.size();
        for(int i=1; i<=n; ++i) {
            if(nums[i-1] == 0) {
                // sum0: the number of subsequences ending with all previous 0s
                // sum0 + 1:  the number of subsequence ending with the current 0
                sum0 += sum0 + 1;
                sum0 %= mod;
            }
            else if(nums[i-1] == 1) {
                // sum1: the number of subsequences ending with all previous 1s
                // sum1 + sum0:  the number of subsequence ending with the current 1
                sum1 += sum1 + sum0;
                sum1 %= mod;
            }
            else {
                // sum2: the number of subsequences ending with all previous 2s
                // sum2 + sum1:  the number of subsequence ending with the current 2
                sum2 += sum2 + sum1;
                sum2 %= mod;
            }
        }
        return sum2;
    }
};


Upper Layer

Comments

Popular posts from this blog

Brute Force - Question 2

2105. Watering Plants II Alice and Bob want to water n plants in their garden. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. Each plant needs a specific amount of water. Alice and Bob have a watering can each, initially full. They water the plants in the following way: Alice waters the plants in order from left to right, starting from the 0th plant. Bob waters the plants in order from right to left, starting from the (n - 1)th plant. They begin watering the plants simultaneously. It takes the same amount of time to water each plant regardless of how much water it needs. Alice/Bob must water the plant if they have enough in their can to fully water it. Otherwise, they first refill their can (instantaneously) then water the plant. In case both Alice and Bob reach the same plant, the one with more water currently in his/her watering can should water this plant. If they have the same amount of water, then Alice ...

Sliding Window - Question 1

Leetcode 76. Minimum Window Substring Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "". The testcases will be generated such that the answer is unique. A substring is a contiguous sequence of characters within the string. Constraints: m == s.length n == t.length 1 <= m, n <= 10^5 s and t consist of uppercase and lowercase English letters. Analysis: The first step to do is to count the letters in string t, since the relative order of chars does not matter. After the counting, we can use two pointers method on string s: starting with beginning of the string s, the second pointer can continue to move until the sub-string [i, j) has all the chars in t, where i the index of the first pointer, and j is the index of the second pointer. We are looking for the sub-string with the minimum leng...