Skip to content

Commit a824e7e

Browse files
committed
1955. Count Number of Special Subsequences: RETRY
1 parent 2fc66b4 commit a824e7e

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

Diff for: src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1473,3 +1473,4 @@ mod s1948_delete_duplicate_folders_in_system;
14731473
mod s1952_three_divisors;
14741474
mod s1953_maximum_number_of_weeks_for_which_you_can_work;
14751475
mod s1954_minimum_garden_perimeter_to_collect_enough_apples;
1476+
mod s1955_count_number_of_special_subsequences;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
*/
44+
pub 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+
51+
impl Solution {
52+
pub fn count_special_subsequences(nums: Vec<i32>) -> i32 {
53+
0
54+
}
55+
}
56+
57+
// submission codes end
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_1955_example_1() {
65+
let nums = vec![0, 1, 2, 2];
66+
67+
let result = 3;
68+
69+
assert_eq!(Solution::count_special_subsequences(nums), result);
70+
}
71+
72+
#[test]
73+
fn test_1955_example_2() {
74+
let nums = vec![2, 2, 0, 0];
75+
76+
let result = 0;
77+
78+
assert_eq!(Solution::count_special_subsequences(nums), result);
79+
}
80+
81+
#[test]
82+
fn test_1955_example_3() {
83+
let nums = vec![0, 1, 2, 0, 1, 2];
84+
85+
let result = 7;
86+
87+
assert_eq!(Solution::count_special_subsequences(nums), result);
88+
}
89+
}

0 commit comments

Comments
 (0)