📌  相关文章
📜  到达终点的最少跳跃次数 |设置 2(O(n) 解决方案)

📅  最后修改于: 2021-09-17 06:48:37             🧑  作者: Mango

给定一个整数数组,其中每个元素表示可以从该元素向前进行的最大步数。编写一个函数,返回到达数组末尾(从第一个元素开始)的最少跳转次数。如果一个元素是 0,那么我们就不能穿过那个元素。如果我们无法到达终点,则返回-1。
例子:

Input:  arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9}
Output: 3 (1-> 3 -> 8 -> 9)
Explanation: Jump from 1st element to 
2nd element as there is only 1 step, 
now there are three options 5, 8 or 9. 
If 8 or 9 is chosen then the end node 9 
can be reached. So 3 jumps are made.

Input:  arr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
Output: 10
Explanation: In every step a jump is 
needed so the count of jumps is 10.

在这篇文章中,将讨论它的 O(n) 解决方案。

在 Set -1 中,讨论了 O(n 2 ) 解决方案。
执行:
要使用的变量:

  1. maxReach变量 maxReach 始终存储数组中的最大可达索引。
  2. step变量 step 存储我们仍然可以采取的步数(并使用索引 0 处的值进行初始化,即初始步数)
  3. jump jump 存储到达最大可到达位置所需的跳跃量。

给定数组 arr = 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9

  • maxReach = arr[0]; // arr[0] = 1,所以我们现在可以达到的最大索引是 1。
    = arr[0]; // arr[0] = 1,我们还能走的步数也是1。
    跳跃= 1; // 我们总是需要至少跳一次。
  • 现在,从索引 1 开始迭代,上述值更新如下:
    1. 首先我们测试我们是否已经到达数组的末尾,在这种情况下我们只需要返回跳转变量。
if (i == arr.length - 1)
    return jump;

2. 接下来我们更新 maxReach。这等于 maxReach 和 i+arr[i](我们可以从当前位置走的步数)的最大值。

maxReach = Math.max(maxReach, i+arr[i]);

3. 我们用了一步来到达当前索引,所以必须减少步数。

step--;

4.如果没有剩余的步数(即steps=0,那么我们一定是使用了跳转。因此增加跳转。既然我们知道有可能以某种方式达到maxReach,我们再次将步数初始化为要达到的步数maxReach 从位置 i。但是在重新初始化 step 之前,我们还会检查 step 是否变为零或为负。在这种情况下,不可能进一步到达。

if (step == 0) {
    jump++;
    if(i>=maxReach)
       return -1;
    step = maxReach - i;
} 
C++
// C++ program to count Minimum number
// of jumps to reach end
#include 
using namespace std;
 
int max(int x, int y)
{
    return (x > y) ? x : y;
}
 
// Returns minimum number of jumps
// to reach arr[n-1] from arr[0]
int minJumps(int arr[], int n)
{
 
    // The number of jumps needed to
    // reach the starting index is 0
    if (n <= 1)
        return 0;
 
    // Return -1 if not possible to jump
    if (arr[0] == 0)
        return -1;
 
    // initialization
    // stores all time the maximal
    // reachable index in the array.
    int maxReach = arr[0];
 
    // stores the number of steps
    // we can still take
    int step = arr[0];
 
    // stores the number of jumps
    // necessary to reach that maximal
    // reachable position.
    int jump = 1;
 
    // Start traversing array
    int i = 1;
    for (i = 1; i < n; i++) {
        // Check if we have reached the end of the array
        if (i == n - 1)
            return jump;
 
        // updating maxReach
        maxReach = max(maxReach, i + arr[i]);
 
        // we use a step to get to the current index
        step--;
 
        // If no further steps left
        if (step == 0) {
            // we must have used a jump
            jump++;
 
            // Check if the current index/position or lesser index
            // is the maximum reach point from the previous indexes
            if (i >= maxReach)
                return -1;
 
            // re-initialize the steps to the amount
            // of steps to reach maxReach from position i.
            step = maxReach - i;
        }
    }
 
    return -1;
}
 
// Driver program to test above function
int main()
{
    int arr[] = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
    int size = sizeof(arr) / sizeof(int);
 
    // Calling the minJumps function
    cout << ("Minimum number of jumps to reach end is %d ",
             minJumps(arr, size));
    return 0;
}
// This code is contributed by
// Shashank_Sharma


C
// C program to count Minimum number
// of jumps to reach end
#include 
 
int max(int x, int y) { return (x > y) ? x : y; }
 
// Returns minimum number of jumps
// to reach arr[n-1] from arr[0]
int minJumps(int arr[], int n)
{
 
    // The number of jumps needed to
    // reach the starting index is 0
    if (n <= 1)
        return 0;
 
    // Return -1 if not possible to jump
    if (arr[0] == 0)
        return -1;
 
    // initialization
    // stores all time the maximal
    // reachable index in the array.
    int maxReach = arr[0];
 
    // stores the number of steps
    // we can still take
    int step = arr[0];
 
    // stores the number of jumps
    // necessary to reach that maximal
    // reachable position.
    int jump = 1;
 
    // Start traversing array
    int i = 1;
    for (i = 1; i < n; i++) {
        // Check if we have reached the end of the array
        if (i == n - 1)
            return jump;
 
        // updating maxReach
        maxReach = max(maxReach, i + arr[i]);
 
        // we use a step to get to the current index
        step--;
 
        // If no further steps left
        if (step == 0) {
            // we must have used a jump
            jump++;
 
            // Check if the current index/position or lesser index
            // is the maximum reach point from the previous indexes
            if (i >= maxReach)
                return -1;
 
            // re-initialize the steps to the amount
            // of steps to reach maxReach from position i.
            step = maxReach - i;
        }
    }
    return -1;
}
 
// Driver program to test above function
int main()
{
    int arr[] = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
    int size = sizeof(arr) / sizeof(int);
 
    // Calling the minJumps function
    printf(
        "Minimum number of jumps to reach end is %d ",
        minJumps(arr, size));
    return 0;
}
// This code is contributed by Abhishek Kumar Singh


Java
// Java program to count Minimum number
// of jumps to reach end
 
class Test {
    static int minJumps(int arr[])
    {
        if (arr.length <= 1)
            return 0;
 
        // Return -1 if not possible to jump
        if (arr[0] == 0)
            return -1;
 
        // initialization
        int maxReach = arr[0];
        int step = arr[0];
        int jump = 1;
 
        // Start traversing array
        for (int i = 1; i < arr.length; i++) {
            // Check if we have reached
// the end of the array
            if (i == arr.length - 1)
                return jump;
 
            // updating maxReach
            maxReach = Math.max(maxReach, i + arr[i]);
 
            // we use a step to get to the current index
            step--;
 
            // If no further steps left
            if (step == 0) {
                // we must have used a jump
                jump++;
 
                // Check if the current
// index/position or lesser index
                // is the maximum reach point
// from the previous indexes
                if (i >= maxReach)
                    return -1;
 
                // re-initialize the steps to the amount
                // of steps to reach maxReach from position i.
                step = maxReach - i;
            }
        }
 
        return -1;
    }
 
    // Driver method to test the above function
    public static void main(String[] args)
    {
        int arr[] = new int[] { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
 
        // calling minJumps method
        System.out.println(minJumps(arr));
    }
}


Python
# python program to count Minimum number
# of jumps to reach end
  
# Returns minimum number of jumps to reach arr[n-1] from arr[0]
def minJumps(arr, n):
  # The number of jumps needed to reach the starting index is 0
  if (n <= 1):
    return 0
  
  # Return -1 if not possible to jump
  if (arr[0] == 0):
    return -1
  
  # initialization
  # stores all time the maximal reachable index in the array
  maxReach = arr[0] 
  # stores the amount of steps we can still take
  step = arr[0]
  # stores the amount of jumps necessary to reach that maximal reachable position
  jump = 1
  
  # Start traversing array
  
  for i in range(1, n):
    # Check if we have reached the end of the array
    if (i == n-1):
      return jump
  
    # updating maxReach
    maxReach = max(maxReach, i + arr[i])
  
    # we use a step to get to the current index
    step -= 1;
  
    # If no further steps left
    if (step == 0):
      # we must have used a jump
      jump += 1
       
      # Check if the current index / position or lesser index
      # is the maximum reach point from the previous indexes
      if(i >= maxReach):
        return -1
  
      # re-initialize the steps to the amount
      # of steps to reach maxReach from position i.
      step = maxReach - i;
  return -1
  
 
# Driver program to test above function
arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]
size = len(arr)
  
# Calling the minJumps function
print("Minimum number of jumps to reach end is % d " % minJumps(arr, size))
 
# This code is contributed by Aditi Sharma


C#
// C# program to count Minimum
// number of jumps to reach end
using System;
 
class GFG {
    static int minJumps(int[] arr)
    {
        if (arr.Length <= 1)
            return 0;
 
        // Return -1 if not
        // possible to jump
        if (arr[0] == 0)
            return -1;
 
        // initialization
        int maxReach = arr[0];
        int step = arr[0];
        int jump = 1;
 
        // Start traversing array
        for (int i = 1; i < arr.Length; i++) {
            // Check if we have reached
            // the end of the array
            if (i == arr.Length - 1)
                return jump;
 
            // updating maxReach
            maxReach = Math.Max(maxReach, i + arr[i]);
 
            // we use a step to get
            // to the current index
            step--;
 
            // If no further steps left
            if (step == 0) {
                // we must have used a jump
                jump++;
 
                // Check if the current index/position
                // or lesser index is the maximum reach
                // point from the previous indexes
                if (i >= maxReach)
                    return -1;
 
                // re-initialize the steps to
                // the amount of steps to reach
                // maxReach from position i.
                step = maxReach - i;
            }
        }
 
        return -1;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = new int[] { 1, 3, 5, 8, 9, 2,
                                6, 7, 6, 8, 9 };
 
        // calling minJumps method
        Console.Write(minJumps(arr));
    }
}
 
// This code is contributed
// by nitin mittal


PHP
= $maxReach)
                return -1;
  
            // re-initialize the steps to the amount
            // of steps to reach maxReach from position i.
            $step = $maxReach - $i;
        }
    }
  
    return -1;
}
  
// Driver program to test above function
 
$arr=array(1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9);
$size = sizeof($arr)/sizeof($arr[0]);
  
// Calling the minJumps function
echo "Minimum number of jumps to reach end is "
     . minJumps($arr, $size);
return 0;
// This code is contribute by Ita_c.
?>


Javascript


C++
// C++ program to illustrate Minimum
// number of jumps to reach end
#include 
using namespace std;
 
// Returns minimum number of jumps
// to reach arr[n-1] from arr[0]
int minJumps(int arr[], int n)
{
    // The number of jumps needed to
    // reach the starting index is 0
    if (n <= 1)
        return 0;
 
    // Return -1 if not possible to jump
    if (arr[0] == 0)
        return -1;
 
    // Stores the number of jumps
    // necessary to reach that maximal
    // reachable position.
    int jump = 1;
 
    // Stores the subarray last index
    int subArrEndIndex = arr[0];
 
    int i = 1;
 
    // Maximum steps covers in
    // first half of sub array
    int subArrFistHalfMaxSteps = 0;
 
    // Maximum steps covers
    // in second half of sub array
    int subArrSecondHalfMaxSteps = 0;
   
    // Start traversing array
    for (i = 1; i < n;) {
 
        subArrEndIndex = i + subArrEndIndex;
       
        // Check if we have reached
        // the end of the array
        if (subArrEndIndex >= n)
            return jump;
 
        int firstHalfMaxStepIndex = 0;
       
        // Iterate the sub array
        // and find out the maxsteps
        // cover index
        for (; i < subArrEndIndex; i++) {
            int stepsCanCover = arr[i] + i;
            if (subArrFistHalfMaxSteps < stepsCanCover) {
                subArrFistHalfMaxSteps = stepsCanCover;
                subArrSecondHalfMaxSteps = 0;
                firstHalfMaxStepIndex = i;
            }
            else if (subArrSecondHalfMaxSteps
                     < stepsCanCover) {
                subArrSecondHalfMaxSteps = stepsCanCover;
            }
        }
        if (i > subArrFistHalfMaxSteps)
            return -1;
        jump++;
       
        // Next subarray end index
        // and so far calculated sub
        // array max step cover value
        subArrEndIndex = arr[firstHalfMaxStepIndex];
        subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps;
    }
 
    return -1;
}
 
// Driver program to test above function
int main()
{
    int arr[] = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
    int size = sizeof(arr) / sizeof(int);
 
    // Calling the minJumps function
    cout << ("Minimum number of jumps to reach end is %d ",
             minJumps(arr, size));
    return 0;
}
// This code is contributed by praveen.kanike


Java
// Java program to illustrate Minimum
// number of jumps to reach end
import java.io.*;
class GFG {
 
  // Returns minimum number of jumps
  // to reach arr[n-1] from arr[0]
  static int minJumps(int arr[], int n)
  {
 
    // The number of jumps needed to
    // reach the starting index is 0
    if (n <= 1)
      return 0;
 
    // Return -1 if not possible to jump
    if (arr[0] == 0)
      return -1;
 
    // Stores the number of jumps
    // necessary to reach that maximal
    // reachable position.
    int jump = 1;
 
    // Stores the subarray last index
    int subArrEndIndex = arr[0];
 
    int i = 1;
 
    // Maximum steps covers in
    // first half of sub array
    int subArrFistHalfMaxSteps = 0;
 
    // Maximum steps covers
    // in second half of sub array
    int subArrSecondHalfMaxSteps = 0;
 
    // Start traversing array
    for (i = 1; i < n;) {
 
      subArrEndIndex = i + subArrEndIndex;
 
      // Check if we have reached
      // the end of the array
      if (subArrEndIndex >= n)
        return jump;
 
      int firstHalfMaxStepIndex = 0;
 
      // Iterate the sub array
      // and find out the maxsteps
      // cover index
      for (; i < subArrEndIndex; i++) {
        int stepsCanCover = arr[i] + i;
        if (subArrFistHalfMaxSteps < stepsCanCover) {
          subArrFistHalfMaxSteps = stepsCanCover;
          subArrSecondHalfMaxSteps = 0;
          firstHalfMaxStepIndex = i;
        }
        else if (subArrSecondHalfMaxSteps
                 < stepsCanCover) {
          subArrSecondHalfMaxSteps = stepsCanCover;
        }
      }
      if (i > subArrFistHalfMaxSteps)
        return -1;
      jump++;
 
      // Next subarray end index
      // and so far calculated sub
      // array max step cover value
      subArrEndIndex = arr[firstHalfMaxStepIndex];
      subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps;
    }
 
    return -1;
  }
 
  // Driver program to test above function
  public static void main (String[] args)
  {
 
    int arr[] = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
    int size=arr.length;
 
    // Calling the minJumps function
    System.out.println("Minimum number of jumps to reach end is "+minJumps(arr, size));
  }
}
 
// This code is contributed by rag2127


C#
// C# program to illustrate Minimum
// number of jumps to reach end
using System;
public class GFG
{
 
  // Returns minimum number of jumps
  // to reach arr[n-1] from arr[0]
  static int minJumps(int[] arr, int n)
  {
 
    // The number of jumps needed to
    // reach the starting index is 0
    if (n <= 1)
      return 0;
 
    // Return -1 if not possible to jump
    if (arr[0] == 0)
      return -1;
 
    // Stores the number of jumps
    // necessary to reach that maximal
    // reachable position.
    int jump = 1;
 
    // Stores the subarray last index
    int subArrEndIndex = arr[0];
 
    int i = 1;
 
    // Maximum steps covers in
    // first half of sub array
    int subArrFistHalfMaxSteps = 0;
 
    // Maximum steps covers
    // in second half of sub array
    int subArrSecondHalfMaxSteps = 0;
 
    // Start traversing array
    for (i = 1; i < n;) {
 
      subArrEndIndex = i + subArrEndIndex;
 
      // Check if we have reached
      // the end of the array
      if (subArrEndIndex >= n)
        return jump;
 
      int firstHalfMaxStepIndex = 0;
 
      // Iterate the sub array
      // and find out the maxsteps
      // cover index
      for (; i < subArrEndIndex; i++)
      {
        int stepsCanCover = arr[i] + i;
        if (subArrFistHalfMaxSteps < stepsCanCover)
        {
          subArrFistHalfMaxSteps = stepsCanCover;
          subArrSecondHalfMaxSteps = 0;
          firstHalfMaxStepIndex = i;
        }
        else if (subArrSecondHalfMaxSteps
                 < stepsCanCover)
        {
          subArrSecondHalfMaxSteps = stepsCanCover;
        }
      }
      if (i > subArrFistHalfMaxSteps)
        return -1;
      jump++;
 
      // Next subarray end index
      // and so far calculated sub
      // array max step cover value
      subArrEndIndex = arr[firstHalfMaxStepIndex];
      subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps;
    }
 
    return -1;
  }
 
  // Driver code
  static public void Main ()
  {
    int[] arr = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
    int size = arr.Length;
 
    // Calling the minJumps function
    Console.WriteLine("Minimum number of jumps to reach end is " +
                      minJumps(arr, size));
  }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出
3

复杂度分析:

  • 时间复杂度: O(n)。
    只需要遍历一次数组。
  • 辅助空间: O(1)。
    不需要空间。

另一种方法:

  1. 为了解决到达数组末尾的最小跳跃,
  2. 对于每个跳转索引,我们考虑需要评估索引中对应的步长值,并使用索引值将数组划分为子部分并找出覆盖索引的最大步数。
  3. 下面的代码和解释会给你一个清晰的概念:
  4. 在每个子数组中找出最大距离覆盖索引作为数组的第一部分,第二个数组

C++

// C++ program to illustrate Minimum
// number of jumps to reach end
#include 
using namespace std;
 
// Returns minimum number of jumps
// to reach arr[n-1] from arr[0]
int minJumps(int arr[], int n)
{
    // The number of jumps needed to
    // reach the starting index is 0
    if (n <= 1)
        return 0;
 
    // Return -1 if not possible to jump
    if (arr[0] == 0)
        return -1;
 
    // Stores the number of jumps
    // necessary to reach that maximal
    // reachable position.
    int jump = 1;
 
    // Stores the subarray last index
    int subArrEndIndex = arr[0];
 
    int i = 1;
 
    // Maximum steps covers in
    // first half of sub array
    int subArrFistHalfMaxSteps = 0;
 
    // Maximum steps covers
    // in second half of sub array
    int subArrSecondHalfMaxSteps = 0;
   
    // Start traversing array
    for (i = 1; i < n;) {
 
        subArrEndIndex = i + subArrEndIndex;
       
        // Check if we have reached
        // the end of the array
        if (subArrEndIndex >= n)
            return jump;
 
        int firstHalfMaxStepIndex = 0;
       
        // Iterate the sub array
        // and find out the maxsteps
        // cover index
        for (; i < subArrEndIndex; i++) {
            int stepsCanCover = arr[i] + i;
            if (subArrFistHalfMaxSteps < stepsCanCover) {
                subArrFistHalfMaxSteps = stepsCanCover;
                subArrSecondHalfMaxSteps = 0;
                firstHalfMaxStepIndex = i;
            }
            else if (subArrSecondHalfMaxSteps
                     < stepsCanCover) {
                subArrSecondHalfMaxSteps = stepsCanCover;
            }
        }
        if (i > subArrFistHalfMaxSteps)
            return -1;
        jump++;
       
        // Next subarray end index
        // and so far calculated sub
        // array max step cover value
        subArrEndIndex = arr[firstHalfMaxStepIndex];
        subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps;
    }
 
    return -1;
}
 
// Driver program to test above function
int main()
{
    int arr[] = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
    int size = sizeof(arr) / sizeof(int);
 
    // Calling the minJumps function
    cout << ("Minimum number of jumps to reach end is %d ",
             minJumps(arr, size));
    return 0;
}
// This code is contributed by praveen.kanike

Java

// Java program to illustrate Minimum
// number of jumps to reach end
import java.io.*;
class GFG {
 
  // Returns minimum number of jumps
  // to reach arr[n-1] from arr[0]
  static int minJumps(int arr[], int n)
  {
 
    // The number of jumps needed to
    // reach the starting index is 0
    if (n <= 1)
      return 0;
 
    // Return -1 if not possible to jump
    if (arr[0] == 0)
      return -1;
 
    // Stores the number of jumps
    // necessary to reach that maximal
    // reachable position.
    int jump = 1;
 
    // Stores the subarray last index
    int subArrEndIndex = arr[0];
 
    int i = 1;
 
    // Maximum steps covers in
    // first half of sub array
    int subArrFistHalfMaxSteps = 0;
 
    // Maximum steps covers
    // in second half of sub array
    int subArrSecondHalfMaxSteps = 0;
 
    // Start traversing array
    for (i = 1; i < n;) {
 
      subArrEndIndex = i + subArrEndIndex;
 
      // Check if we have reached
      // the end of the array
      if (subArrEndIndex >= n)
        return jump;
 
      int firstHalfMaxStepIndex = 0;
 
      // Iterate the sub array
      // and find out the maxsteps
      // cover index
      for (; i < subArrEndIndex; i++) {
        int stepsCanCover = arr[i] + i;
        if (subArrFistHalfMaxSteps < stepsCanCover) {
          subArrFistHalfMaxSteps = stepsCanCover;
          subArrSecondHalfMaxSteps = 0;
          firstHalfMaxStepIndex = i;
        }
        else if (subArrSecondHalfMaxSteps
                 < stepsCanCover) {
          subArrSecondHalfMaxSteps = stepsCanCover;
        }
      }
      if (i > subArrFistHalfMaxSteps)
        return -1;
      jump++;
 
      // Next subarray end index
      // and so far calculated sub
      // array max step cover value
      subArrEndIndex = arr[firstHalfMaxStepIndex];
      subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps;
    }
 
    return -1;
  }
 
  // Driver program to test above function
  public static void main (String[] args)
  {
 
    int arr[] = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
    int size=arr.length;
 
    // Calling the minJumps function
    System.out.println("Minimum number of jumps to reach end is "+minJumps(arr, size));
  }
}
 
// This code is contributed by rag2127

C#

// C# program to illustrate Minimum
// number of jumps to reach end
using System;
public class GFG
{
 
  // Returns minimum number of jumps
  // to reach arr[n-1] from arr[0]
  static int minJumps(int[] arr, int n)
  {
 
    // The number of jumps needed to
    // reach the starting index is 0
    if (n <= 1)
      return 0;
 
    // Return -1 if not possible to jump
    if (arr[0] == 0)
      return -1;
 
    // Stores the number of jumps
    // necessary to reach that maximal
    // reachable position.
    int jump = 1;
 
    // Stores the subarray last index
    int subArrEndIndex = arr[0];
 
    int i = 1;
 
    // Maximum steps covers in
    // first half of sub array
    int subArrFistHalfMaxSteps = 0;
 
    // Maximum steps covers
    // in second half of sub array
    int subArrSecondHalfMaxSteps = 0;
 
    // Start traversing array
    for (i = 1; i < n;) {
 
      subArrEndIndex = i + subArrEndIndex;
 
      // Check if we have reached
      // the end of the array
      if (subArrEndIndex >= n)
        return jump;
 
      int firstHalfMaxStepIndex = 0;
 
      // Iterate the sub array
      // and find out the maxsteps
      // cover index
      for (; i < subArrEndIndex; i++)
      {
        int stepsCanCover = arr[i] + i;
        if (subArrFistHalfMaxSteps < stepsCanCover)
        {
          subArrFistHalfMaxSteps = stepsCanCover;
          subArrSecondHalfMaxSteps = 0;
          firstHalfMaxStepIndex = i;
        }
        else if (subArrSecondHalfMaxSteps
                 < stepsCanCover)
        {
          subArrSecondHalfMaxSteps = stepsCanCover;
        }
      }
      if (i > subArrFistHalfMaxSteps)
        return -1;
      jump++;
 
      // Next subarray end index
      // and so far calculated sub
      // array max step cover value
      subArrEndIndex = arr[firstHalfMaxStepIndex];
      subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps;
    }
 
    return -1;
  }
 
  // Driver code
  static public void Main ()
  {
    int[] arr = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
    int size = arr.Length;
 
    // Calling the minJumps function
    Console.WriteLine("Minimum number of jumps to reach end is " +
                      minJumps(arr, size));
  }
}
 
// This code is contributed by avanitrachhadiya2155

Javascript


输出
3

时间复杂度: O(n)。

辅助空间: O(1)。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程