Neural Mastery
← Back to Practice

Longest Increasing Subsequence

Difficulty: Hard · Pattern: Dynamic Programming · Concept: General Coding (DSA) — Core Patterns to Drill

Given a list of integers, find the length of the longest strictly increasing subsequence (elements don't need to be contiguous — just appear in increasing order and in their original relative positions).

Maximum Non-Adjacent Sum needed only O(1)O(1) state per step (include vs. exclude the previous element). This problem's naive DP needs O(n)O(n) state per element (the best subsequence length ending at each specific index), giving O(n2)O(n^2) overall. There's a genuinely different, faster technique — patience sorting, using binary search — that gets it down to O(nlogn)O(n \log n), the same complexity class jump K-Means's assignment step doesn't need but a real LIS implementation over large inputs does.

Your task: implement longest_increasing_subsequence(nums) in O(nlogn)O(n \log n) time, returning the length (not the subsequence itself).

Implement it yourself
assert longest_increasing_subsequence([10, 9, 2, 5, 3, 7, 101, 18]) == 4 assert longest_increasing_subsequence([0, 1, 0, 3, 2, 3]) == 4 assert longest_increasing_subsequence([7, 7, 7, 7]) == 1 assert longest_increasing_subsequence([]) == 0 assert longest_increasing_subsequence([1, 2, 3, 4, 5]) == 5

Next: The Two Lone Numbers

Last updated Sep 5, 2026Edit this pageReport an issue