Lv.2arrayhashsorting
Longest Consecutive Run
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Return the length of the longest run of consecutive integers (x, x+1, …) whose values all appear in nums (order and
duplicates do not matter).
Examples
| nums | result |
|---|---|
[100, 4, 200, 1, 3, 2] | 4 |
[0, 3, 7, 2, 5, 8, 4, 6, 0, 1] | 9 |
[5, 5, 5] | 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)
- Space
- O(N)
Examples
| nums | Expected |
|---|---|
[100,4,200,1,3,2] | 4 |
[0,3,7,2,5,8,4,6,0,1] | 9 |
[5,5,5] | 1 |
Hint
Hint
Sort and scan, or put everything in a hash set and only start counting at values x where x - 1 is absent. Checking
membership by scanning the array is .
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 | 1232 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 | 8 | verdict only |
| performance | 60 | 4 | verdict only |