Count Number of Special Subsequences Solution Leetcode United states 2021
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
- For example,
- In contrast,
[0,1,2,0]are not special.
Given an array
nums (consisting of only integers
2), return the number of different subsequences that are special. Since the answer may be very large, return it modulo
109 + 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.
Input: nums = [0,1,2,2] Output: 3 Explanation: The special subsequences are [0,1,2,2], [0,1,2,2], and [0,1,2,2].
Input: nums = [2,2,0,0] Output: 0 Explanation: There are no special subsequences in [2,2,0,0].
Input: nums = [0,1,2,0,1,2] Output: 7 Explanation: The special subsequences are: - [0,1,2,0,1,2] - [0,1,2,0,1,2] - [0,1,2,0,1,2] - [0,1,2,0,1,2] - [0,1,2,0,1,2] - [0,1,2,0,1,2] - [0,1,2,0,1,2]
1 <= nums.length <= 105
0 <= nums[i] <= 2