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.
A sequence is special if it consists of a positive number of 0s, followed by a positive number of 1s, then a positive number of 2s.
[0,1,2] and [0,0,1,1,1,2] are special.[2,1,0], [1], and [0,1,2,0] are not special.Given an array nums (consisting of only integers 0, 1, and 2), return the number of different subsequences that are special. Since the answer may be very large, return it modulo 109 + 7.
A subsequence 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 different if the set of indices chosen are different.
Example 1:
Input: nums = [0,1,2,2] Output: 3 Explanation: The special subsequences are bolded [0,1,2,2], [0,1,2,2], and [0,1,2,2].
Example 2:
Input: nums = [2,2,0,0] Output: 0 Explanation: There are no special subsequences in [2,2,0,0].
Example 3:
Input: nums = [0,1,2,0,1,2] Output: 7 Explanation: The special subsequences are bolded: - [0,1,2,0,1,2] - [0,1,2,0,1,2] - [0,1,2,0,1,2] - [0,1,2,0,1,2] - [0,1,2,0,1,2] - [0,1,2,0,1,2] - [0,1,2,0,1,2]
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 2Problem summary: A sequence is special if it consists of a positive number of 0s, followed by a positive number of 1s, then a positive number of 2s. For example, [0,1,2] and [0,0,1,1,1,2] are special. In contrast, [2,1,0], [1], and [0,1,2,0] are not special. Given an array nums (consisting of only integers 0, 1, and 2), return the number of different subsequences that are special. Since the answer may be very large, return it modulo 109 + 7. A subsequence 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 different if the set of indices chosen are different.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[0,1,2,2]
[2,2,0,0]
[0,1,2,0,1,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1955: Count Number of Special Subsequences
class Solution {
public int countSpecialSubsequences(int[] nums) {
final int mod = (int) 1e9 + 7;
int n = nums.length;
int[][] f = new int[n][3];
f[0][0] = nums[0] == 0 ? 1 : 0;
for (int i = 1; i < n; ++i) {
if (nums[i] == 0) {
f[i][0] = (2 * f[i - 1][0] % mod + 1) % mod;
f[i][1] = f[i - 1][1];
f[i][2] = f[i - 1][2];
} else if (nums[i] == 1) {
f[i][0] = f[i - 1][0];
f[i][1] = (f[i - 1][0] + 2 * f[i - 1][1] % mod) % mod;
f[i][2] = f[i - 1][2];
} else {
f[i][0] = f[i - 1][0];
f[i][1] = f[i - 1][1];
f[i][2] = (f[i - 1][1] + 2 * f[i - 1][2] % mod) % mod;
}
}
return f[n - 1][2];
}
}
// Accepted solution for LeetCode #1955: Count Number of Special Subsequences
func countSpecialSubsequences(nums []int) int {
const mod = 1e9 + 7
n := len(nums)
f := make([][3]int, n)
if nums[0] == 0 {
f[0][0] = 1
}
for i := 1; i < n; i++ {
if nums[i] == 0 {
f[i][0] = (2*f[i-1][0] + 1) % mod
f[i][1] = f[i-1][1]
f[i][2] = f[i-1][2]
} else if nums[i] == 1 {
f[i][0] = f[i-1][0]
f[i][1] = (f[i-1][0] + 2*f[i-1][1]) % mod
f[i][2] = f[i-1][2]
} else {
f[i][0] = f[i-1][0]
f[i][1] = f[i-1][1]
f[i][2] = (f[i-1][1] + 2*f[i-1][2]) % mod
}
}
return f[n-1][2]
}
# Accepted solution for LeetCode #1955: Count Number of Special Subsequences
class Solution:
def countSpecialSubsequences(self, nums: List[int]) -> int:
mod = 10**9 + 7
n = len(nums)
f = [[0] * 3 for _ in range(n)]
f[0][0] = nums[0] == 0
for i in range(1, n):
if nums[i] == 0:
f[i][0] = (2 * f[i - 1][0] + 1) % mod
f[i][1] = f[i - 1][1]
f[i][2] = f[i - 1][2]
elif nums[i] == 1:
f[i][0] = f[i - 1][0]
f[i][1] = (f[i - 1][0] + 2 * f[i - 1][1]) % mod
f[i][2] = f[i - 1][2]
else:
f[i][0] = f[i - 1][0]
f[i][1] = f[i - 1][1]
f[i][2] = (f[i - 1][1] + 2 * f[i - 1][2]) % mod
return f[n - 1][2]
// Accepted solution for LeetCode #1955: Count Number of Special Subsequences
/**
* [1955] Count Number of Special Subsequences
*
* A sequence is special if it consists of a positive number of 0s, followed by a positive number of 1s, then a positive number of 2s.
*
* For example, [0,1,2] and [0,0,1,1,1,2] are special.
* In contrast, [2,1,0], [1], and [0,1,2,0] are not special.
*
* Given an array nums (consisting of only integers 0, 1, and 2), return the number of different subsequences that are special. Since the answer may be very large, return it modulo 10^9 + 7.
* A subsequence 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 different if the set of indices chosen are different.
*
* Example 1:
*
* Input: nums = [0,1,2,2]
* Output: 3
* Explanation: The special subsequences are bolded [<u>0</u>,<u>1</u>,<u>2</u>,2], [<u>0</u>,<u>1</u>,2,<u>2</u>], and [<u>0</u>,<u>1</u>,<u>2</u>,<u>2</u>].
*
* Example 2:
*
* Input: nums = [2,2,0,0]
* Output: 0
* Explanation: There are no special subsequences in [2,2,0,0].
*
* Example 3:
*
* Input: nums = [0,1,2,0,1,2]
* Output: 7
* Explanation: The special subsequences are bolded:
* - [<u>0</u>,<u>1</u>,<u>2</u>,0,1,2]
* - [<u>0</u>,<u>1</u>,2,0,1,<u>2</u>]
* - [<u>0</u>,<u>1</u>,<u>2</u>,0,1,<u>2</u>]
* - [<u>0</u>,<u>1</u>,2,0,<u>1</u>,<u>2</u>]
* - [<u>0</u>,1,2,<u>0</u>,<u>1</u>,<u>2</u>]
* - [<u>0</u>,1,2,0,<u>1</u>,<u>2</u>]
* - [0,1,2,<u>0</u>,<u>1</u>,<u>2</u>]
*
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* 0 <= nums[i] <= 2
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/count-number-of-special-subsequences/
// discuss: https://leetcode.com/problems/count-number-of-special-subsequences/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn count_special_subsequences(nums: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1955_example_1() {
let nums = vec![0, 1, 2, 2];
let result = 3;
assert_eq!(Solution::count_special_subsequences(nums), result);
}
#[test]
#[ignore]
fn test_1955_example_2() {
let nums = vec![2, 2, 0, 0];
let result = 0;
assert_eq!(Solution::count_special_subsequences(nums), result);
}
#[test]
#[ignore]
fn test_1955_example_3() {
let nums = vec![0, 1, 2, 0, 1, 2];
let result = 7;
assert_eq!(Solution::count_special_subsequences(nums), result);
}
}
// Accepted solution for LeetCode #1955: Count Number of Special Subsequences
function countSpecialSubsequences(nums: number[]): number {
const mod = 1e9 + 7;
const n = nums.length;
const f: number[][] = Array(n)
.fill(0)
.map(() => Array(3).fill(0));
f[0][0] = nums[0] === 0 ? 1 : 0;
for (let i = 1; i < n; ++i) {
if (nums[i] === 0) {
f[i][0] = (((2 * f[i - 1][0]) % mod) + 1) % mod;
f[i][1] = f[i - 1][1];
f[i][2] = f[i - 1][2];
} else if (nums[i] === 1) {
f[i][0] = f[i - 1][0];
f[i][1] = (f[i - 1][0] + ((2 * f[i - 1][1]) % mod)) % mod;
f[i][2] = f[i - 1][2];
} else {
f[i][0] = f[i - 1][0];
f[i][1] = f[i - 1][1];
f[i][2] = (f[i - 1][1] + ((2 * f[i - 1][2]) % mod)) % mod;
}
}
return f[n - 1][2];
}
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.