Longest Increasing Subsequence
Difficulty: Medium
Given an integer array nums, return the length of the longest strictly increasing subsequence. A subsequence keeps the original left-to-right order but can skip numbers. Each number must be strictly greater than the one before it.
Examples
Input: nums = [10, 9, 2, 5, 3, 7, 101, 18]
Output: 4
One such subsequence is [2, 3, 7, 101]; another is [2, 3, 7, 18].
Input: nums = [0, 1, 0, 3, 2, 3]
Output: 4
[0, 1, 2, 3] is a valid increasing subsequence of length 4.
Input: nums = [7, 7, 7, 7]
Output: 1
Since it must be strictly increasing, repeats can't extend a subsequence past length 1.
Constraints
1 <= nums.length <= 2500
-10^4 <= nums[i] <= 10^4
Approach
For each index i, ask: how long can a subsequence ending exactly at nums[i] be? It's 1 (nums[i] alone) plus the best "ends here" answer among earlier, smaller numbers. The best over all indexes is the answer, but comparing every index with every earlier one costs O(n^2).
A faster way doesn't track which number extends which subsequence. Keep a list, "tails", where tails[k] is the smallest last value of any increasing subsequence of length k + 1 so far. "tails" stays sorted, so binary-search it for the first value not smaller than the new number. The number either extends the list (a new longest length) or replaces that entry with a smaller, more promising tail. The final length of "tails" is the answer.
Solutions
Brute Force — Plain Recursion
Recursively try including or skipping each number (including only ones that keep the sequence increasing), and keep the longest result.
function lengthOfLIS(nums) {
function longestFrom(i, prevValue) {
let best = 0;
for (let j = i; j < nums.length; j++) {
if (nums[j] > prevValue) {
best = Math.max(best, 1 + longestFrom(j + 1, nums[j]));
}
}
return best;
}
return longestFrom(0, -Infinity);
}Time: O(2^n) · Space: O(n) — recursion depth
Better — Bottom-Up DP
For each index, compute the longest increasing subsequence ending there by checking every earlier index with a smaller value.
function lengthOfLIS(nums) {
const n = nums.length;
const dp = new Array(n).fill(1); // every number alone is a subsequence of length 1
let best = 1;
for (let i = 1; i < n; i++) {
for (let j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
best = Math.max(best, dp[i]);
}
return best;
}Time: O(n^2) · Space: O(n)
Optimal — Binary Search on Tails
Keep the smallest possible tail value for an increasing subsequence of each length. Binary-search where each new number fits: it either extends the longest subsequence or improves a shorter one's tail.
function lengthOfLIS(nums) {
const tails = [];
for (const num of nums) {
let low = 0;
let high = tails.length;
// Binary search for the first tail value >= num
while (low < high) {
const mid = Math.floor((low + high) / 2);
if (tails[mid] < num) {
low = mid + 1;
} else {
high = mid;
}
}
if (low === tails.length) {
tails.push(num); // num extends the longest subsequence found so far
} else {
tails[low] = num; // num gives a smaller, more promising tail for this length
}
}
return tails.length;
}Time: O(n log n) · Space: O(n)