Lv.3binary-searchdp
Longest Increasing Subsequence
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Given an integer array nums, return the length of the longest strictly increasing subsequence (elements kept in their
original order).
Examples
| nums | result |
|---|---|
[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
-
nums.length -
nums[i]
Expected complexity
- Time
- O(N log N)
- Space
- O(N)
Examples
| nums | Expected |
|---|---|
[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 DP exceeds the time
limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1054 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 10 | verdict only |
| performance | 60 | 4 | verdict only |