You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>We define <span class="arithmatex">\(f[i][j]\)</span> to represent the number of special subsequences ending with <span class="arithmatex">\(j\)</span> among the first <span class="arithmatex">\(i+1\)</span> elements. Initially, <span class="arithmatex">\(f[i][j]=0\)</span>, and if <span class="arithmatex">\(nums[0]=0\)</span>, then <span class="arithmatex">\(f[0][0]=1\)</span>.</p>
87162
+
<p>For <span class="arithmatex">\(i \gt 0\)</span>, we consider the value of <span class="arithmatex">\(nums[i]\)</span>:</p>
87163
+
<p>If <span class="arithmatex">\(nums[i] = 0\)</span>: If we do not choose <span class="arithmatex">\(nums[i]\)</span>, then <span class="arithmatex">\(f[i][0] = f[i-1][0]\)</span>; if we choose <span class="arithmatex">\(nums[i]\)</span>, then <span class="arithmatex">\(f[i][0]=f[i-1][0]+1\)</span>, because we can add a <span class="arithmatex">\(0\)</span> to the end of any special subsequence ending with <span class="arithmatex">\(0\)</span> to get a new special subsequence, or we can use <span class="arithmatex">\(nums[i]\)</span> alone as a special subsequence. Therefore, <span class="arithmatex">\(f[i][0] = 2 \times f[i - 1][0] + 1\)</span>. The rest of <span class="arithmatex">\(f[i][j]\)</span> is equal to <span class="arithmatex">\(f[i-1][j]\)</span>.</p>
87164
+
<p>If <span class="arithmatex">\(nums[i] = 1\)</span>: If we do not choose <span class="arithmatex">\(nums[i]\)</span>, then <span class="arithmatex">\(f[i][1] = f[i-1][1]\)</span>; if we choose <span class="arithmatex">\(nums[i]\)</span>, then <span class="arithmatex">\(f[i][1]=f[i-1][1]+f[i-1][0]\)</span>, because we can add a <span class="arithmatex">\(1\)</span> to the end of any special subsequence ending with <span class="arithmatex">\(0\)</span> or <span class="arithmatex">\(1\)</span> to get a new special subsequence. Therefore, <span class="arithmatex">\(f[i][1] = f[i-1][0] + 2 \times f[i - 1][1]\)</span>. The rest of <span class="arithmatex">\(f[i][j]\)</span> is equal to <span class="arithmatex">\(f[i-1][j]\)</span>.</p>
87165
+
<p>If <span class="arithmatex">\(nums[i] = 2\)</span>: If we do not choose <span class="arithmatex">\(nums[i]\)</span>, then <span class="arithmatex">\(f[i][2] = f[i-1][2]\)</span>; if we choose <span class="arithmatex">\(nums[i]\)</span>, then <span class="arithmatex">\(f[i][2]=f[i-1][2]+f[i-1][1]\)</span>, because we can add a <span class="arithmatex">\(2\)</span> to the end of any special subsequence ending with <span class="arithmatex">\(1\)</span> or <span class="arithmatex">\(2\)</span> to get a new special subsequence. Therefore, <span class="arithmatex">\(f[i][2] = f[i-1][1] + 2 \times f[i - 1][2]\)</span>. The rest of <span class="arithmatex">\(f[i][j]\)</span> is equal to <span class="arithmatex">\(f[i-1][j]\)</span>.</p>
87166
+
<p>In summary, we can get the following state transition equations:</p>
<p>The final answer is <span class="arithmatex">\(f[n-1][2]\)</span>.</p>
87176
+
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, and the space complexity is <span class="arithmatex">\(O(n)\)</span>. Where <span class="arithmatex">\(n\)</span> is the length of the array <span class="arithmatex">\(nums\)</span>.</p>
<p>We notice that in the above state transition equations, the value of <span class="arithmatex">\(f[i][j]\)</span> is only related to <span class="arithmatex">\(f[i-1][j]\)</span>. Therefore, we can remove the first dimension and optimize the space complexity to <span class="arithmatex">\(O(1)\)</span>.</p>
87434
+
<p>We can use an array <span class="arithmatex">\(f\)</span> 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 <span class="arithmatex">\(f\)</span> according to the value of the current element.</p>
87435
+
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, and the space complexity is <span class="arithmatex">\(O(1)\)</span>. Where <span class="arithmatex">\(n\)</span> is the length of the array <span class="arithmatex">\(nums\)</span>.</p>
0 commit comments