|
76 | 76 |
|
77 | 77 | <!-- solution:start -->
|
78 | 78 |
|
79 |
| -### Solution 1 |
| 79 | +### Solution 1: Dynamic Programming |
| 80 | + |
| 81 | +We define $f[i][j]$ to represent the number of special subsequences ending with $j$ among the first $i+1$ elements. Initially, $f[i][j]=0$, and if $nums[0]=0$, then $f[0][0]=1$. |
| 82 | + |
| 83 | +For $i \gt 0$, we consider the value of $nums[i]$: |
| 84 | + |
| 85 | +If $nums[i] = 0$: If we do not choose $nums[i]$, then $f[i][0] = f[i-1][0]$; if we choose $nums[i]$, then $f[i][0]=f[i-1][0]+1$, because we can add a $0$ to the end of any special subsequence ending with $0$ to get a new special subsequence, or we can use $nums[i]$ alone as a special subsequence. Therefore, $f[i][0] = 2 \times f[i - 1][0] + 1$. The rest of $f[i][j]$ is equal to $f[i-1][j]$. |
| 86 | + |
| 87 | +If $nums[i] = 1$: If we do not choose $nums[i]$, then $f[i][1] = f[i-1][1]$; if we choose $nums[i]$, then $f[i][1]=f[i-1][1]+f[i-1][0]$, because we can add a $1$ to the end of any special subsequence ending with $0$ or $1$ to get a new special subsequence. Therefore, $f[i][1] = f[i-1][0] + 2 \times f[i - 1][1]$. The rest of $f[i][j]$ is equal to $f[i-1][j]$. |
| 88 | + |
| 89 | +If $nums[i] = 2$: If we do not choose $nums[i]$, then $f[i][2] = f[i-1][2]$; if we choose $nums[i]$, then $f[i][2]=f[i-1][2]+f[i-1][1]$, because we can add a $2$ to the end of any special subsequence ending with $1$ or $2$ to get a new special subsequence. Therefore, $f[i][2] = f[i-1][1] + 2 \times f[i - 1][2]$. The rest of $f[i][j]$ is equal to $f[i-1][j]$. |
| 90 | + |
| 91 | +In summary, we can get the following state transition equations: |
| 92 | + |
| 93 | +$$ |
| 94 | +\begin{aligned} |
| 95 | +f[i][0] &= 2 \times f[i - 1][0] + 1, \quad nums[i] = 0 \\ |
| 96 | +f[i][1] &= f[i-1][0] + 2 \times f[i - 1][1], \quad nums[i] = 1 \\ |
| 97 | +f[i][2] &= f[i-1][1] + 2 \times f[i - 1][2], \quad nums[i] = 2 \\ |
| 98 | +f[i][j] &= f[i-1][j], \quad nums[i] \neq j |
| 99 | +\end{aligned} |
| 100 | +$$ |
| 101 | + |
| 102 | +The final answer is $f[n-1][2]$. |
| 103 | + |
| 104 | +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$. |
| 105 | + |
| 106 | +Similar code found with 1 license type |
80 | 107 |
|
81 | 108 | <!-- tabs:start -->
|
82 | 109 |
|
@@ -229,7 +256,13 @@ function countSpecialSubsequences(nums: number[]): number {
|
229 | 256 |
|
230 | 257 | <!-- solution:start -->
|
231 | 258 |
|
232 |
| -### Solution 2 |
| 259 | +### Solution 2: Dynamic Programming (Space Optimization) |
| 260 | + |
| 261 | +We notice that in the above state transition equations, the value of $f[i][j]$ is only related to $f[i-1][j]$. Therefore, we can remove the first dimension and optimize the space complexity to $O(1)$. |
| 262 | + |
| 263 | +We can use an array $f$ of length 3 to represent the number of special subsequences ending with 0, 1, and 2, respectively. For each element in the array, we update the array $f$ according to the value of the current element. |
| 264 | + |
| 265 | +The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the array $nums$. |
233 | 266 |
|
234 | 267 | <!-- tabs:start -->
|
235 | 268 |
|
|
0 commit comments