Leetcode 45: Jump Game II
You are given a 0-indexed array ’nums’ where each element represents the maximum length you can jump forward from that index. You are initially positioned at nums[0], and you need to reach the last index. Return the minimum number of jumps needed to reach the last index.
Problem
Approach
Steps
Complexity
Input: You are given a 0-indexed array of integers 'nums'. Each integer represents the maximum jump length from that index.
Example: Input: nums = [3, 4, 1, 1, 5]
Constraints:
• 1 <= nums.length <= 10^4
• 0 <= nums[i] <= 1000
• It's guaranteed that you can reach nums[n-1].
Output: Return the minimum number of jumps needed to reach the last index of the array.
Example: Output: 2
Constraints:
• The output should be an integer representing the minimum number of jumps.
Goal: The goal is to minimize the number of jumps needed to reach the last index.
Steps:
• Initialize two variables: 'cur' (current index) and 'far' (farthest reachable index) to track your position and the furthest you can jump.
• Iterate over the array, and for each index, update the 'far' index to reflect the maximum distance you can reach from that point.
• When you reach 'cur' (the point where a jump is necessary), update 'cur' to 'far' and increment the jump count.
• Repeat this process until you reach the last index.
Goal: The constraints ensure that the inputs are valid and you can always reach the last index.
Steps:
• 1 <= nums.length <= 10^4
• 0 <= nums[i] <= 1000
• It's guaranteed that you can reach nums[n-1].
Assumptions:
• The input array will always allow a valid path to reach the last index.
• Input: Input: nums = [3, 4, 1, 1, 5]
• Explanation: Start at index 0, jump 3 steps to index 1, then jump 4 steps to reach the last index. This takes 2 jumps.
• Input: Input: nums = [2, 3, 1, 1, 4]
• Explanation: Jump 1 step to index 1, then jump 3 steps to reach the last index, making it a total of 2 jumps.
• Input: Input: nums = [1, 2, 3, 4, 5]
• Explanation: Jump 1 step to index 1, 2 steps to index 3, and 1 step to the last index, resulting in 3 jumps.
Approach: The problem can be approached using a greedy algorithm that minimizes the number of jumps by always jumping to the farthest possible index within each jump.
Observations:
• The key observation is that we need to jump to the farthest reachable index to minimize the number of jumps.
• We can use a greedy strategy: always try to reach the farthest point from the current position in each jump.
Steps:
• Initialize two variables 'cur' (current index) and 'far' (farthest reachable index).
• Iterate through the array, updating the 'far' index for each position.
• When you reach the current jump limit ('cur'), update 'cur' to 'far' and increment the jump count.
• Stop when you reach or exceed the last index.
Empty Inputs:
• If nums has only one element, no jump is needed.
Large Inputs:
• Ensure that the solution works efficiently with large inputs, up to the constraint limit of 10^4 elements.
Special Values:
• Handle the case where nums contains large jump values at the beginning, which could quickly minimize the number of jumps.
Constraints:
• The algorithm must efficiently handle arrays with lengths up to 10^4.
int jump(vector<int>& nums) {
int jumps = 0;
int cur = 0;
int far = 0;
for(int i = 0; i < nums.size() - 1; i++) {
far = max(far, i + nums[i]);
if(i == cur) {
cur = far;
jumps++;
}
}
return jumps;
}
1 : Function Declaration
int jump(vector<int>& nums) {
This line declares a function named `jump` that takes a vector of non-negative integers `nums` as input and returns the minimum number of jumps required to reach the end of the array.
2 : Variable Initialization
int jumps = 0;
This line initializes a variable `jumps` to 0 to keep track of the number of jumps made so far.
3 : Variable Initialization
int cur = 0;
This line initializes a variable `cur` to 0 to represent the current position in the array.
4 : Variable Initialization
int far = 0;
This line initializes a variable `far` to 0 to represent the farthest reachable position from the current position.
5 : Loop Iteration
for(int i = 0; i < nums.size() - 1; i++) {
This line starts a `for` loop to iterate through the array, excluding the last element.
6 : Update Farthest Reachable Position
far = max(far, i + nums[i]);
This line updates the `far` variable to the maximum of its current value and the farthest reachable position from the current index `i`.
7 : Condition Check
if(i == cur) {
This line checks if the current index `i` has reached the current farthest position `cur`.
8 : Update Current Position and Jump Count
cur = far;
jumps++;
If the condition is true, it means we've reached the end of a jump. We update the `cur` position to the new farthest reachable position `far` and increment the `jumps` count.
9 : Return Result
return jumps;
This line returns the final value of `jumps`, which represents the minimum number of jumps required to reach the end of the array.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: In all cases, the solution iterates over the array exactly once, giving it a linear time complexity.
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is constant as the solution only uses a few extra variables.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus