Find the length of the longest increasing subsequence.
This problem follows the LIS DP / Binary Search pattern, commonly found in the 1-D Dynamic Programming category. Recognizing this pattern is key to solving it efficiently in an interview setting.
dp[i] = longest subsequence ending at i. Or use patience sorting with binary search for O(n log n).
The tails array isn't the actual LIS — it tracks the smallest possible tail for each length. Binary search on this array gives O(n log n).
# O(n log n) approach
tails = []
for num in nums:
pos = bisect_left(tails, num)
if pos == len(tails):
tails.append(num)
else:
tails[pos] = num
return len(tails)Practice Longest Increasing Subsequence and similar 1-D Dynamic Programming problems with flashcards. Build pattern recognition through active recall.
Practice this problem