← All Exercises
Longest Increasing Subsequence in O(n log n)
advancedtypescript
Background
Patience sorting models card piles and provides the fastest comparison-based LIS length algorithm.
Problem
Given an integer array, return the length of the longest strictly increasing subsequence, achieving O(n log n) time rather than the quadratic DP.
Input
An array of up to 250000 integers, possibly with duplicates and negative values.
Output
A single integer: the maximum length of a strictly increasing subsequence.
Sample tests
Sample 1
Input: nums = [10,9,2,5,3,7,101,18]
Expected: 4
Sample 2
Input: nums = [0,1,0,3,2,3]
Expected: 4
Sample 3
Input: nums = [7,7,7]
Expected: 1
Your solution
Learning objective
Maintain patience piles with a lower-bound search and prove that the pile count equals the LIS length.