Longest Increasing Subsequence
A medium Dynamic Programming problem included in Apna College, Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Dynamic Programming
- Sheets
- 3
- Core for
- 9 roles
- Platform
- LeetCode
The problem
Given an integer array, return the length of the longest strictly increasing subsequence. A subsequence is derived by deleting some or no elements without changing the order of remaining elements.
Example 1
- Input
- nums = [10,9,2,5,3,7,101,18]
- Output
- 4
- Why
- The longest increasing subsequence is [2,3,7,101], so length is 4.
Example 2
- Input
- nums = [0,1,0,3,2,3]
- Output
- 4
- Why
- One longest increasing subsequence is [0,1,2,3], length 4.
Example 3
- Input
- nums = [7,7,7,7,7,7,7]
- Output
- 1
- Why
- All elements are equal, so the longest strictly increasing subsequence has length 1.
Constraints
- 1 <= nums.length <= 2500
- -10^4 <= nums[i] <= 10^4
How to think about it
Updated 2026-09-09When extending an increasing subsequence, the specific past elements do not matter: only the tail element matters, because smaller tails make room for more future numbers. Keeping track of the smallest possible tail for every subsequence length produces a sorted list of barriers where each new number can be placed via binary search.
Approaches, worst first
Subsequence generation
time O(2^n) · space O(n)
Generate all 2^n subsequences, verify which ones are strictly increasing, and track the maximum length. The exponential explosion makes this impossible for n up to 2500.
Prefix maximum scan
time O(n^2) · space O(n)
Let dp[i] be the length of the longest increasing subsequence ending at index i. For each i, scan all j < i where nums[j] < nums[i] and take max(dp[j]) + 1. Correct and standard, but quadratic in runtime.
Patience sorting with binary searchWrite this one
time O(n log n) · space O(n)
Maintain an array `tails` where tails[len] stores the smallest ending value among all increasing subsequences of length len + 1 seen so far. For each number, binary search for its insertion point: either replace the first tail >= num, or append if greater than all.
Where people lose marks · 3
- Confusing strictly increasing with non-decreasing. When nums[i] equals a tail element, it cannot extend the subsequence; binary search must look for lower_bound (first >=) rather than upper_bound.
- Assuming the `tails` array itself contains the actual longest increasing subsequence. It stores minimal tail values across different valid subsequences, not a single sequence.
- Seeding the DP array with 0 instead of 1. A single element by itself always forms a valid increasing subsequence of length 1.
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.
What each operation costs
| Operation | Time |
|---|---|
| fill dynamic programming table of n states | O(n) |
| solve two-dimensional grid of m by n states | O(m * n) |
| space-optimized state transition keeping one row | O(n) |
What usually goes wrong with Dynamic Programming
- Filling a bottom-up table in an order where the current cell needs values that have not been computed yet, reading uninitialized zeros.
- Failing to initialize base cases properly, such as filling a minimization table with zeros instead of infinity, which traps the answer at zero.
- Overwriting values in a 1D space-optimized knapsack array by scanning in the wrong direction, allowing the same item to be chosen multiple times.
Which roles need this problem
Dynamic Programming is a core topic for these 9 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 5 more roles, including Game Developer, Cryptography Engineer, Performance Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Dynamic Programming problems
Problem set and role mapping as of .