Lv.2arraybfsgreedy
Minimum Jumps
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Stones are laid in a row; from stone i you may jump forward to any stone from i + 1 to i + nums[i]. Starting at stone
0, return the minimum number of jumps to reach the last stone (n - 1), or -1 if it cannot be reached (0 if n = 1).
Examples
| nums | result |
|---|---|
[2, 3, 1, 1, 4] | 2 |
[3, 2, 1, 0, 4] | -1 |
[0] | 0 |
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(1)
Examples
| nums | Expected |
|---|---|
[2,3,1,1,4] | 2 |
[3,2,1,0,4] | -1 |
[0] | 0 |
Hint
Hint
Think BFS over ranges: the stones reachable with k + 1 jumps end at the farthest i + nums[i] over the range reachable with
k jumps. Track end, far and jumps in one pass. Relaxing every reachable cell from every stone is
and 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 | 1018 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 | 11 | verdict only |
| performance | 60 | 5 | verdict only |