1955. Count Number of Special Subsequences Hard

@problem@discussion
#Array#Dynamic Programming



1/**
2 * [1955] Count Number of Special Subsequences
3 *
4 * 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.
5 * 
6 * 	For example, [0,1,2] and [0,0,1,1,1,2] are special.
7 * 	In contrast, [2,1,0], [1], and [0,1,2,0] are not special.
8 * 
9 * 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.
10 * 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.
11 *  
12 * Example 1:
13 * 
14 * Input: nums = [0,1,2,2]
15 * Output: 3
16 * Explanation: The special subsequences are bolded [<u>0</u>,<u>1</u>,<u>2</u>,2], [<u>0</u>,<u>1</u>,2,<u>2</u>], and [<u>0</u>,<u>1</u>,<u>2</u>,<u>2</u>].
17 * 
18 * Example 2:
19 * 
20 * Input: nums = [2,2,0,0]
21 * Output: 0
22 * Explanation: There are no special subsequences in [2,2,0,0].
23 * 
24 * Example 3:
25 * 
26 * Input: nums = [0,1,2,0,1,2]
27 * Output: 7
28 * Explanation: The special subsequences are bolded:
29 * - [<u>0</u>,<u>1</u>,<u>2</u>,0,1,2]
30 * - [<u>0</u>,<u>1</u>,2,0,1,<u>2</u>]
31 * - [<u>0</u>,<u>1</u>,<u>2</u>,0,1,<u>2</u>]
32 * - [<u>0</u>,<u>1</u>,2,0,<u>1</u>,<u>2</u>]
33 * - [<u>0</u>,1,2,<u>0</u>,<u>1</u>,<u>2</u>]
34 * - [<u>0</u>,1,2,0,<u>1</u>,<u>2</u>]
35 * - [0,1,2,<u>0</u>,<u>1</u>,<u>2</u>]
36 * 
37 *  
38 * Constraints:
39 * 
40 * 	1 <= nums.length <= 10^5
41 * 	0 <= nums[i] <= 2
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/count-number-of-special-subsequences/
47// discuss: https://leetcode.com/problems/count-number-of-special-subsequences/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn count_special_subsequences(nums: Vec<i32>) -> i32 {
53        0
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_1955() {
65    }
66}
67


Back
© 2025 bowen.ge All Rights Reserved.