Funcia
Lv.3binary-searchdp

Longest Increasing Subsequence

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
0

Given an integer array nums, return the length of the longest strictly increasing subsequence (elements kept in their original order).

Examples

numsresult
[10, 9, 2, 5, 3, 7, 101, 18]4
[0, 1, 0, 3, 2, 3]4
[7, 7, 7, 7]1

Efficiency

This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.

Function signature

solution(nums: int[]) → int

Constraints

  • 1≤1 \le nums.length ≤200,000\le 200{,}000
  • −109≤-10^9 \le nums[i] ≤109\le 10^9

Expected complexity

Time
O(N log N)
Space
O(N)

Examples

numsExpected
[10,9,2,5,3,7,101,18]4
[0,1,0,3,2,3]4
[7,7,7,7]1

Hint

Hint

Maintain tails[L], the smallest possible last value of an increasing subsequence of length L + 1; it stays sorted. For each x, replace the first element >= x (lower bound) or append. The answer is len(tails). The O(N2)O(N^2) DP exceeds the time limit.

Time limits per language

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251054 msderived
Kotlin 2.4 (JVM)1400 msestimated
C# 14 (.NET 10)1400 msestimated

Test groups

GroupWeightTestsVisibility
sample03inputs and outputs
correctness4010verdict only
performance604verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.