Longest Increasing Subsequence
Medium
Topics
Given an integer array nums, return the length of the longest strictly increasing subsequence.
Example 1
one longest increasing subsequence
The shaded cells increase left to right but need not be adjacent — that is what separates a subsequence from a subarray. Length 4.
Cells are the array values; the small numbers underneath are indices.
Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: [2,3,7,101] or [2,3,7,18].
Example 2
one longest increasing subsequence
Input: nums = [0,1,0,3,2,3] Output: 4
Constraints
- 1 <= nums.length <= 2500
- -10^4 <= nums[i] <= 10^4
Editorial — intuition, approach, complexity and pitfalls
Intuition
The O(n²) DP tries every previous element. The faster idea: keep an array tails where tails[k] is the smallest tail value of any increasing subsequence of length k+1.
A smaller tail is always at least as useful (it leaves more room to extend), so binary-searching each number into tails keeps it sorted, and its length is the LIS length. This is patience sorting.
Approach
- For each number, binary-search the first tail ≥ it.
- If none is ≥ it, it extends the longest run → append.
- Otherwise replace that tail with this (smaller) value.
- The length of
tailsis the answer.
Walkthrough
nums = [10, 9, 2, 5, 3, 7]
10 -> [10]
9 -> [9]
2 -> [2]
5 -> [2,5]
3 -> [2,3]
7 -> [2,3,7] -> length 3Complexity
- Time — O(n log n): one binary search per element.
- Space — O(n): the tails array.
Common Pitfalls
tailsis not itself a valid subsequence — only its length is meaningful.- Use
bisect_leftfor strictly increasing;bisect_rightwould allow duplicates. mid = (lo + hi) / 2is the textbook form, and in Java or C++ it is the one that bites: the momentlo + hipasses2^31 - 1it wraps negative and the index flies out of range. This problem’s bounds never get close, so the code above is correct as written, butlo + (hi - lo) / 2is the habit worth keeping — same answer, same cost, no overflow. Python’s integers are arbitrary precision, so it is a non-issue there.
Full Solution — all languages
class Solution:
def lengthOfLIS(self, nums):
import bisect
tails = []
for x in nums:
i = bisect.bisect_left(tails, x)
if i == len(tails):
tails.append(x)
else:
tails[i] = x
return len(tails)import java.util.*;
class Solution {
public int lengthOfLIS(int[] nums) {
List<Integer> tails = new ArrayList<>();
for (int x : nums) {
int lo = 0, hi = tails.size();
while (lo < hi) {
int mid = (lo + hi) / 2;
if (tails.get(mid) < x) lo = mid + 1;
else hi = mid;
}
if (lo == tails.size()) tails.add(x);
else tails.set(lo, x);
}
return tails.size();
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int lengthOfLIS(vector<int>& nums) {
vector<int> tails;
for (int x : nums) {
auto it = lower_bound(tails.begin(), tails.end(), x);
if (it == tails.end()) tails.push_back(x);
else *it = x;
}
return (int)tails.size();
}
};/**
* @param {number[]} nums
* @return {number}
*/ var lengthOfLIS = function(nums) {
const tails = [];
for (const x of nums) {
let lo = 0, hi = tails.length;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (tails[mid] < x) lo = mid + 1;
else hi = mid;
}
if (lo === tails.length) tails.push(x);
else tails[lo] = x;
}
return tails.length;
};