Number of Longest Increasing Subsequence
A medium Dynamic Programming interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Dynamic Programming
- Sheets
- 1
- Core for
- 9 roles
The problem
Given an integer array, return the number of longest increasing subsequences. Note that the sequence must be strictly increasing.
Example 1
- Input
- nums = [1,3,5,4,7]
- Output
- 2
- Why
- The two longest increasing subsequences are [1,3,4,7] and [1,3,5,7].
Example 2
- Input
- nums = [2,2,2,2,2]
- Output
- 5
- Why
- Every single element is a longest increasing subsequence of length 1, so there are 5.
Example 3
- Input
- nums = [1]
- Output
- 1
- Why
- Only one element, one longest increasing subsequence.
Constraints
- 1 <= nums.length <= 2000
- -10^6 <= nums[i] <= 10^6
How to think about it
Updated 2026-09-09Finding the maximum length is only half the problem; whenever an earlier subsequence matches the best length ending at index i, its count must be accumulated, and whenever a strictly longer subsequence is discovered, the count must reset to that predecessor's count.
Approaches, worst first
Subsequence collection
time O(2^n) · space O(n)
Generate all valid strictly increasing subsequences, filter for the maximum length found, and count them. Exponential in time and space.
Dual dynamic programming arrays
time O(n^2) · space O(n)
Maintain `lengths` and `counts` arrays of size n, initialized to 1. For each i, scan j < i: if nums[j] < nums[i], update lengths[i] and counts[i]. If lengths[j] + 1 > lengths[i], reset lengths[i] and counts[i] = counts[j]; if equal, add counts[j] to counts[i].
Segment tree with prefix queriesWrite this one
time O(n log n) · space O(n)
Coordinate-compress values and maintain a segment tree storing (max_len, count). For each num, query the range [min_val, num - 1] to obtain the best previous length and its combined count.
Where people lose marks · 3
- Resetting counts incorrectly: when lengths[j] + 1 == lengths[i], counts[i] must increase by counts[j], not increment by 1.
- Summing counts only from the last element: multiple distinct indices can terminate longest increasing subsequences of max_length; all such counts must be summed.
- All duplicate elements (e.g., [2,2,2]): strictly increasing requires length 1, yielding total count n.
The theory behind it
Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems
What Dynamic Programming is
Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.
When to reach for it
Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.
How the pattern works
Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.
More Dynamic Programming problems
Target Roles
Core requirement for 9 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .