Skip to content

Commit 1853b88

Browse files
committed
feat: add solution 1955. Count Number of Special Subsequences
1 parent 2bb7c0e commit 1853b88

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [1955. Count Number of Special Subsequences](https://leetcode.com/problems/count-number-of-special-subsequences)
2+
3+
## Description
4+
5+
<div class="elfjS" data-track-load="description_content"><p>A sequence is <strong>special</strong> if it consists of a <strong>positive</strong> number of <code>0</code>s, followed by a <strong>positive</strong> number of <code>1</code>s, then a <strong>positive</strong> number of <code>2</code>s.</p>
6+
7+
<ul>
8+
<li>For example, <code>[0,1,2]</code> and <code>[0,0,1,1,1,2]</code> are special.</li>
9+
<li>In contrast, <code>[2,1,0]</code>, <code>[1]</code>, and <code>[0,1,2,0]</code> are not special.</li>
10+
</ul>
11+
12+
<p>Given an array <code>nums</code> (consisting of <strong>only</strong> integers <code>0</code>, <code>1</code>, and <code>2</code>), return<em> the <strong>number of different subsequences</strong> that are special</em>. Since the answer may be very large, <strong>return it modulo </strong><code>10<sup>9</sup> + 7</code>.</p>
13+
14+
<p>A <strong>subsequence</strong> 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 <strong>different</strong> if the <strong>set of indices</strong> chosen are different.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<pre><strong>Input:</strong> nums = [0,1,2,2]
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong> The special subsequences are bolded [<strong><u>0</u></strong>,<strong><u>1</u></strong>,<strong><u>2</u></strong>,2], [<strong><u>0</u></strong>,<strong><u>1</u></strong>,2,<strong><u>2</u></strong>], and [<strong><u>0</u></strong>,<strong><u>1</u></strong>,<strong><u>2</u></strong>,<strong><u>2</u></strong>].
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre><strong>Input:</strong> nums = [2,2,0,0]
27+
<strong>Output:</strong> 0
28+
<strong>Explanation:</strong> There are no special subsequences in [2,2,0,0].
29+
</pre>
30+
31+
<p><strong class="example">Example 3:</strong></p>
32+
33+
<pre><strong>Input:</strong> nums = [0,1,2,0,1,2]
34+
<strong>Output:</strong> 7
35+
<strong>Explanation:</strong> The special subsequences are bolded:
36+
- [<strong><u>0</u></strong>,<strong><u>1</u></strong>,<strong><u>2</u></strong>,0,1,2]
37+
- [<strong><u>0</u></strong>,<strong><u>1</u></strong>,2,0,1,<strong><u>2</u></strong>]
38+
- [<strong><u>0</u></strong>,<strong><u>1</u></strong>,<strong><u>2</u></strong>,0,1,<strong><u>2</u></strong>]
39+
- [<strong><u>0</u></strong>,<strong><u>1</u></strong>,2,0,<strong><u>1</u></strong>,<strong><u>2</u></strong>]
40+
- [<strong><u>0</u></strong>,1,2,<strong><u>0</u></strong>,<strong><u>1</u></strong>,<strong><u>2</u></strong>]
41+
- [<strong><u>0</u></strong>,1,2,0,<strong><u>1</u></strong>,<strong><u>2</u></strong>]
42+
- [0,1,2,<strong><u>0</u></strong>,<strong><u>1</u></strong>,<strong><u>2</u></strong>]
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
50+
<li><code>0 &lt;= nums[i] &lt;= 2</code></li>
51+
</ul>
52+
</div>
53+
54+
<p>&nbsp;</p>
55+
56+
## Solutions
57+
58+
**Solution: `Dynamic Programming`**
59+
60+
- Time complexity: <em>O(n)</em>
61+
- Space complexity: <em>O(1)</em>
62+
63+
<p>&nbsp;</p>
64+
65+
### **JavaScript**
66+
67+
```js
68+
/**
69+
* @param {number[]} nums
70+
* @return {number}
71+
*/
72+
const countSpecialSubsequences = function (nums) {
73+
const MODULO = 10 ** 9 + 7;
74+
const dp = [1, 0, 0, 0];
75+
76+
for (const num of nums) {
77+
dp[num + 1] = (dp[num + 1] * 2 + dp[num]) % MODULO;
78+
}
79+
80+
return dp[3];
81+
};
82+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const countSpecialSubsequences = function (nums) {
6+
const MODULO = 10 ** 9 + 7;
7+
const dp = [1, 0, 0, 0];
8+
9+
for (const num of nums) {
10+
dp[num + 1] = (dp[num + 1] * 2 + dp[num]) % MODULO;
11+
}
12+
13+
return dp[3];
14+
};

0 commit comments

Comments
 (0)