You 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] andi + j < nReturn 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: 2Explanation: 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 <= 1040 <= nums[i] <= 1000It's guaranteed that you can reach nums[n - 1].
Question
You 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] andi + j < nReturn 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: 2Explanation: 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 <= 1040 <= nums[i] <= 1000It's guaranteed that you can reach nums[n - 1].
Solution
To solve this problem, we can use a greedy algorithm. Here are the steps:
-
Initialize two variables,
jumpsandfarthest, to 0.jumpswill keep track of the minimum number of jumps needed to reach the end, andfarthestwill keep track of the farthest index we can reach with the current number of jumps. -
Iterate over the array from the start to the second last element. For each element, update
farthestto be the maximum offarthestandi + nums[i], whereiis the current index. -
Initialize a variable
endto 0.endwill keep track of the end of the current jump. If we reachend, it means we have to make another jump to proceed further. So, wheniequalsend, incrementjumpsby 1 and updateendtofarthest. -
Continue this process until we reach the end of the array.
5
Similar Questions
Minimum Number of Jumps to Reach End Given an array of integers where each element represents the maximum number of steps that can be jumped going forward from that element, write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). If the end is not reachable, return -1.Constraints:NAExample:Sample Input:523114Sample Output:2Explanation:First line of input--> size of array (n)Then n lines of integers.Output--> From the above example ,the number of jumps needed to reach the end of array are 2Public Test Cases:# INPUT EXPECTED OUTPUT1 5231142
Have the function ArrayChallenge(arr) take the array of numbers stored in arr and first determine the largest element in the array, and then determine whether or not you can reach that same element within the array by moving left or right continuously according to whatever integer is in the current spot. If you can reach the same spot within the array, then your program should output the least amount of jumps it took. For example: if the input is [2, 3, 5, 6, 1] you'll start at the spot where 6 is and if you jump 6 spaces to the right while looping around the array you end up at the last element where the 1 is. Then from here you jump 1 space to the left and you're back where you started, so your program should output 2. If it's impossible to end up back at the largest element in the array your program should output -1. The largest element in the array will never equal the number of elements in the array. The largest element will be unique.
Java Program To Find Minimum No. of Jumps to Reach The End of an Array
Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.If target is not found in the array, return [-1, -1].You must write an algorithm with O(log n) runtime complexity. Example 1:Input: nums = [5,7,7,8,8,10], target = 8Output: [3,4]Example 2:Input: nums = [5,7,7,8,8,10], target = 6Output: [-1,-1]Example 3:Input: nums = [], target = 0Output: [-1,-1] Constraints:0 <= nums.length <= 105-109 <= nums[i] <= 109nums is a non-decreasing array.-109 <= target <= 109
You are given an integer array nums.In one move, you can choose one element of nums and change it to any value.Return the minimum difference between the largest and smallest value of nums after performing at most three moves.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.