|
| 1 | +<h2><a href="https://leetcode.com/problems/number-of-stable-subsequences">Number of Stable Subsequences</a></h2> <img src='https://img.shields.io/badge/Difficulty-Hard-red' alt='Difficulty: Hard' /><hr><p>You are given an integer array <code>nums</code>.</p> |
| 2 | + |
| 3 | +<p>A <strong><span data-keyword="subsequence-array-nonempty">subsequence</span></strong> is <strong>stable</strong> if it does not contain <strong>three consecutive</strong> elements with the <strong>same</strong> parity when the subsequence is read <strong>in order</strong> (i.e., consecutive <strong>inside the subsequence</strong>).</p> |
| 4 | + |
| 5 | +<p>Return the number of stable subsequences.</p> |
| 6 | + |
| 7 | +<p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<div class="example-block"> |
| 13 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,3,5]</span></p> |
| 14 | + |
| 15 | +<p><strong>Output:</strong> <span class="example-io">6</span></p> |
| 16 | + |
| 17 | +<p><strong>Explanation:</strong></p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>Stable subsequences are <code>[1]</code>, <code>[3]</code>, <code>[5]</code>, <code>[1, 3]</code>, <code>[1, 5]</code>, and <code>[3, 5]</code>.</li> |
| 21 | + <li>Subsequence <code>[1, 3, 5]</code> is not stable because it contains three consecutive odd numbers. Thus, the answer is 6.</li> |
| 22 | +</ul> |
| 23 | +</div> |
| 24 | + |
| 25 | +<p><strong class="example">Example 2:</strong></p> |
| 26 | + |
| 27 | +<div class="example-block"> |
| 28 | +<p><strong>Input:</strong> <span class="example-io">nums = </span>[2,3,4,2]</p> |
| 29 | + |
| 30 | +<p><strong>Output:</strong> <span class="example-io">14</span></p> |
| 31 | + |
| 32 | +<p><strong>Explanation:</strong></p> |
| 33 | + |
| 34 | +<ul> |
| 35 | + <li>The only subsequence that is not stable is <code>[2, 4, 2]</code>, which contains three consecutive even numbers.</li> |
| 36 | + <li>All other subsequences are stable. Thus, the answer is 14.</li> |
| 37 | +</ul> |
| 38 | +</div> |
| 39 | + |
| 40 | +<p> </p> |
| 41 | +<p><strong>Constraints:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 45 | + <li><code>1 <= nums[i] <= 10<sup>5</sup></code></li> |
| 46 | +</ul> |
0 commit comments