Longest Increasing Subsequence
Find the length of the longest strictly increasing subsequence in an array of integers.
The problem
You get an integer array nums. A subsequence keeps some of the elements in their original order, not necessarily next to each other. Return the length of the longest subsequence whose values strictly increase.
Input nums = [4, 10, 4, 3, 8, 9]
Output 3
4, 8, 9 (or 3, 8, 9). The two 4s can't both be used, since the increase must be strict.
Input nums = [9, 8, 7]
Output 1
Each value is smaller than the one before it, so the best you can do is a single number.
Key constraint: The array has at most 2,500 numbers, so an O(n^2) DP of about 3 million pair checks is fine.
How to spot the pattern
- You pick elements in order, skipping any you like, to build the longest chain with a rule between neighbors.
- The best chain ending at a position extends the best chain ending at some earlier, smaller element.
The idea
State: length[i] is the length of the longest increasing subsequence that ends exactly at nums[i]. Such a subsequence is either nums[i] alone or the best one ending at some earlier nums[j] < nums[i], with nums[i] added. So length[i] = 1 + max(length[j]) over those j, or 1 if there are none, and the answer is the largest length[i].
Brute force: Check all 2^n subsequences for being increasing, which takes O(2^n * n) time.
Step by step
- 1Set
length[i] = 1for every index, since each number alone is an increasing subsequence. - 2For each index
i, look at every earlier indexj. - 3If
nums[j] < nums[i]andlength[j] + 1is bigger thanlength[i], store it. - 4Return the largest value in
length.
See the pattern move
The 1d dynamic programming pattern on a small example. Step through it, or press play.
Code
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
n = len(nums)
# length[i] = longest strictly increasing subsequence that ends at nums[i]
# length[i] = 1 + max(length[j]) over every j < i with nums[j] < nums[i]
length = [1] * n
for i in range(n):
for j in range(i):
if nums[j] < nums[i] and length[j] + 1 > length[i]:
length[i] = length[j] + 1
return max(length)Complexity
Time
O(n^2)
Space
O(n)
Each pair j < i is checked once, and the table holds one length per index.
Watch out for
- Returning
length[n - 1]: the longest subsequence can end anywhere, so take the maximum over all indices. - Using
<=instead of<, which counts equal values as increasing.
Follow-up questions
Can you do it in O(n log n)?
Keep tails, where tails[k] is the smallest last value of any increasing subsequence of length k + 1. For each number, binary search the first tail that is not smaller and replace it, or append if there is none; the length of tails is the answer.
How would you return the subsequence itself?
Store for each i the index j that gave its best length. Start from the index with the largest length and follow these links back, then reverse.
In Hinglish
`length[i]` matlab `nums[i]` par khatam hone wala sabse lamba increasing subsequence. Har chhote pichhle number se `length[j] + 1` try karo, aur end mein poore array ka max lo.