Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an integer array nums.
A subsequence is stable if it does not contain three consecutive elements with the same parity when the subsequence is read in order (i.e., consecutive inside the subsequence).
Return the number of stable subsequences.
Since the answer may be too large, return it modulo 109 + 7.
Example 1:
Input: nums = [1,3,5]
Output: 6
Explanation:
[1], [3], [5], [1, 3], [1, 5], and [3, 5].[1, 3, 5] is not stable because it contains three consecutive odd numbers. Thus, the answer is 6.Example 2:
Input: nums = [2,3,4,2]
Output: 14
Explanation:
[2, 4, 2], which contains three consecutive even numbers.Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 105Problem summary: You are given an integer array nums. A subsequence is stable if it does not contain three consecutive elements with the same parity when the subsequence is read in order (i.e., consecutive inside the subsequence). Return the number of stable subsequences. Since the answer may be too large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,3,5]
[2,3,4,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3686: Number of Stable Subsequences
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3686: Number of Stable Subsequences
// package main
//
// // https://space.bilibili.com/206214
// func countStableSubsequences(nums []int) int {
// const mod = 1_000_000_007
// f := [2][2]int{}
// for _, x := range nums {
// x %= 2
// f[x][1] = (f[x][1] + f[x][0]) % mod
// f[x][0] = (f[x][0] + f[x^1][0] + f[x^1][1] + 1) % mod
// }
// return (f[0][0] + f[0][1] + f[1][0] + f[1][1]) % mod
// }
// Accepted solution for LeetCode #3686: Number of Stable Subsequences
package main
// https://space.bilibili.com/206214
func countStableSubsequences(nums []int) int {
const mod = 1_000_000_007
f := [2][2]int{}
for _, x := range nums {
x %= 2
f[x][1] = (f[x][1] + f[x][0]) % mod
f[x][0] = (f[x][0] + f[x^1][0] + f[x^1][1] + 1) % mod
}
return (f[0][0] + f[0][1] + f[1][0] + f[1][1]) % mod
}
# Accepted solution for LeetCode #3686: Number of Stable Subsequences
#
# @lc app=leetcode id=3686 lang=python3
#
# [3686] Number of Stable Subsequences
#
# @lc code=start
class Solution:
def countStableSubsequences(self, nums: List[int]) -> int:
MOD = 10**9 + 7
# dp[0]: count of stable subsequences ending with 1 even number
# dp[1]: count of stable subsequences ending with 2 even numbers
# dp[2]: count of stable subsequences ending with 1 odd number
# dp[3]: count of stable subsequences ending with 2 odd numbers
dp = [0] * 4
for x in nums:
if x % 2 == 0:
# Current number is Even
# Can form new subsequences ending in 2 evens by appending to those ending in 1 even
added_to_1_even = dp[0]
# Can form new subsequences ending in 1 even by appending to those ending in odds
# Plus the single element subsequence [x]
added_to_odds = (dp[2] + dp[3] + 1) % MOD
dp[1] = (dp[1] + added_to_1_even) % MOD
dp[0] = (dp[0] + added_to_odds) % MOD
else:
# Current number is Odd
# Can form new subsequences ending in 2 odds by appending to those ending in 1 odd
added_to_1_odd = dp[2]
# Can form new subsequences ending in 1 odd by appending to those ending in evens
# Plus the single element subsequence [x]
added_to_evens = (dp[0] + dp[1] + 1) % MOD
dp[3] = (dp[3] + added_to_1_odd) % MOD
dp[2] = (dp[2] + added_to_evens) % MOD
return sum(dp) % MOD
# @lc code=end
// Accepted solution for LeetCode #3686: Number of Stable Subsequences
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3686: Number of Stable Subsequences
// package main
//
// // https://space.bilibili.com/206214
// func countStableSubsequences(nums []int) int {
// const mod = 1_000_000_007
// f := [2][2]int{}
// for _, x := range nums {
// x %= 2
// f[x][1] = (f[x][1] + f[x][0]) % mod
// f[x][0] = (f[x][0] + f[x^1][0] + f[x^1][1] + 1) % mod
// }
// return (f[0][0] + f[0][1] + f[1][0] + f[1][1]) % mod
// }
// Accepted solution for LeetCode #3686: Number of Stable Subsequences
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3686: Number of Stable Subsequences
// package main
//
// // https://space.bilibili.com/206214
// func countStableSubsequences(nums []int) int {
// const mod = 1_000_000_007
// f := [2][2]int{}
// for _, x := range nums {
// x %= 2
// f[x][1] = (f[x][1] + f[x][0]) % mod
// f[x][0] = (f[x][0] + f[x^1][0] + f[x^1][1] + 1) % mod
// }
// return (f[0][0] + f[0][1] + f[1][0] + f[1][1]) % mod
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.