Mercury: Jump Game II
harbor run -t elfsong/mercury-jump-game-iiharbor download elfsong/mercury-jump-game-iiYou are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i] and
i + j < n
Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].
Example 1:
Input: nums = [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [2,3,0,1,4] Output: 2
Constraints:
1 <= nums.length <= 10⁴
0 <= nums[i] <= 1000
It's guaranteed that you can reach nums[n - 1].
Implement Solution.jump in /app/solution.py using Python 3.12.
Keep the supplied class and method signature. The file is the only submitted artifact.
The evaluator instantiates Solution and invokes this method with the problem's inputs;
do not read standard input or write a command-line program.
class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
The standard library, lctk 0.0.6 (LeetCode tree/list helpers), sortedcontainers 2.4.0, NumPy 2.4.3, SciPy 1.17.1, and six 1.17.0 are available. Standard LeetCode imports, including List, Optional, TreeNode and ListNode, are provided by the starter file.
Your solution must pass every hidden test. For a correct solution, the evaluator also measures CPU runtime over 128 repetitions per test using independent copies of the inputs, and compares it with passing reference solutions measured in the same environment. Aim for an efficient algorithm. Wrong solutions receive zero efficiency credit.