📌  相关文章
📜  计算跳到终点的方式数量

📅  最后修改于: 2021-05-06 23:28:22             🧑  作者: Mango

给定一个数字数组,其中每个元素代表可以从该元素转发的最大跳转数。对于每个数组元素,可以计数从该元素到数组末尾的跳转方式。如果元素为0,则无法通过该元素进行移动。无法到达末尾的元素应具有计数“ -1”。

例子:

Input : {3, 2, 0, 1}
Output : 2 1 -1 0
For 3 number of steps or jumps that 
can be taken are 1, 2 or 3. The different ways are:
3 -> 2 -> 1
3 -> 1

For 2 number of steps or jumps that 
can be taken are 1, or 2. The different ways are:
2 -> 1

For 0 number of steps or jumps that 
can be taken are 0. 
One cannot move forward from this point.

For 1 number of steps or jumps that 
can be taken are 1. But the element is at
the end so no jump is required.

Input : {1, 3, 5, 8, 9, 1, 0, 7, 6, 8, 9}
Output : 52 52 28 16 8 -1 -1 4 2 1 0

此问题是到达终点的最小跳转数的一种变化(方法3)。在这里,我们需要计算从每个单元格到达终点的所有方法。

该解决方案是对到达终点的最小跳数(方法3)问题的解决方案的改进版本。
这个问题旨在计算从每个元素跳到终点的不同方式。对于每个元素,通过将可以到达末尾并且当前元素可以达到的所有正向元素的计数加1来计算计数(如果该元素可以直接到达末尾)。

算法:

countWays(arr, n)
    Initialize array count_jump[n] = {0}

    count_jump[n-1] = 0
    for i = n-2 to 0
        if arr[i] >= (n-i-1)
         count_jump[i]++
        for j=i+1; j < n-1 && j <= arr[i]+i; i++
          if count_jump[j] != -1
             count_jump[i] += count_jump[j]
        if count_jump[i] == 0
         count_jump[i] = -1

    for i = 0 to n-1
        print count_jump[i]
C++
// C++ implementation to count number
// of ways to jump to reach end
#include 
using namespace std;
  
// function to count ways to jump to
// reach end for each array element
void countWaysToJump(int arr[], int n)
{
    // count_jump[i] store number of ways
    // arr[i] can reach to the end
    int count_jump[n];
    memset(count_jump, 0, sizeof(count_jump));
  
    // Last element does not require to jump.
    // Count ways to jump for remaining
    // elements
    for (int i=n-2; i>=0; i--)
    {
        // if the element can directly
        // jump to the end
        if (arr[i] >= n - i - 1)
            count_jump[i]++;
  
        // add the count of all the elements
        // that can reach to end and arr[i] can
        // reach to them
        for (int j=i+1; j < n-1 && j <= arr[i] + i; j++)
  
            // if element can reach to end then add
            // its count to count_jump[i]
            if (count_jump[j] != -1)
                 count_jump[i] += count_jump[j];
  
        // if arr[i] cannot reach to the end
        if (count_jump[i] == 0)
            count_jump[i] = -1;
    }
  
    // print count_jump for each
    // array element
    for (int i=0; i


Java
// Java implementation to count number
// of ways to jump to reach end
import java.util.Arrays;
  
class GFG {
      
    // function to count ways to jump to
    // reach end for each array element
    static void countWaysToJump(int arr[], int n)
    {
          
        // count_jump[i] store number of ways
        // arr[i] can reach to the end
        int count_jump[] = new int[n];
        Arrays.fill(count_jump, 0);
      
        // Last element does not require to jump.
        // Count ways to jump for remaining
        // elements
        for (int i = n-2; i >= 0; i--)
        {
              
            // if the element can directly
            // jump to the end
            if (arr[i] >= n - i - 1)
                count_jump[i]++;
      
            // add the count of all the elements
            // that can reach to end and arr[i] can
            // reach to them
            for (int j = i+1; j < n-1 && j <= arr[i] + i; j++)
      
                // if element can reach to end then add
                // its count to count_jump[i]
                if (count_jump[j] != -1)
                    count_jump[i] += count_jump[j];
      
            // if arr[i] cannot reach to the end
            if (count_jump[i] == 0)
                count_jump[i] = -1;
        }
      
        // print count_jump for each
        // array element
        for (int i = 0; i < n; i++)
            System.out.print(count_jump[i] + " ");
    }
      
    //driver code
    public static void main (String[] args)
    {
          
        int arr[] = {1, 3, 5, 8, 9, 1, 0, 7, 6, 8, 9};
        int n = arr.length;
          
        countWaysToJump(arr, n);
    }
}
  
// This code is contributed by Anant Agarwal.


Python3
# Python3 implementation to count 
# number of ways to jump to reach end
  
# Function to count ways to jump to
# reach end for each array element
def countWaysToJump(arr, n):
  
    # count_jump[i] store number of ways
    # arr[i] can reach to the end
    count_jump = [0 for i in range(n)]
  
    # Last element does not require 
    # to jump. Count ways to jump for 
    # remaining elements
    for i in range(n - 2, -1, -1):
      
        # if the element can directly
        # jump to the end
        if (arr[i] >= n - i - 1):
            count_jump[i] += 1
  
        # Add the count of all the elements
        # that can reach to end and arr[i] 
        # can reach to them
        j = i + 1
        while(j < n-1 and j <= arr[i] + i):
  
            # if element can reach to end then 
            # add its count to count_jump[i]
            if (count_jump[j] != -1):
                count_jump[i] += count_jump[j]
            j += 1
              
        # if arr[i] cannot reach to the end
        if (count_jump[i] == 0):
            count_jump[i] = -1
      
  
    # print count_jump for each
    # array element
    for i in range(n):
        print(count_jump[i], end = " ")
  
# Driver code
arr = [1, 3, 5, 8, 9, 1, 0, 7, 6, 8, 9]
n = len(arr)
countWaysToJump(arr, n)
  
# This code is contributed by Anant Agarwal.


C#
// C# implementation to count number 
// of ways to jump to reach end 
using System;
  
class GFG { 
      
    // function to count ways to jump to 
    // reach end for each array element 
    static void countWaysToJump(int[] arr, int n) 
    { 
          
        // count_jump[i] store number of ways 
        // arr[i] can reach to the end 
        int[] count_jump = new int[n];
          
        for(int i = 0; i < n; i++)
            count_jump[i] = 0;
          
      
        // Last element does not require to jump. 
        // Count ways to jump for remaining 
        // elements 
        for (int i = n-2; i >= 0; i--) 
        { 
              
            // if the element can directly 
            // jump to the end 
            if (arr[i] >= n - i - 1) 
                count_jump[i]++; 
      
            // add the count of all the elements 
            // that can reach to end and arr[i] can 
            // reach to them 
            for (int j = i+1; j < n-1 && j <= arr[i] + i; j++) 
      
                // if element can reach to end then add 
                // its count to count_jump[i] 
                if (count_jump[j] != -1) 
                    count_jump[i] += count_jump[j]; 
      
            // if arr[i] cannot reach to the end 
            if (count_jump[i] == 0) 
                count_jump[i] = -1; 
        } 
      
        // print count_jump for each 
        // array element 
        for (int i = 0; i < n; i++) 
            Console.Write(count_jump[i] + " "); 
    } 
      
    // Driver code 
    public static void Main () 
    { 
        int[] arr = {1, 3, 5, 8, 9,
                 1, 0, 7, 6, 8, 9}; 
        int n = arr.Length; 
        countWaysToJump(arr, n); 
    } 
} 
  
// This code is contributed by ChitraNayal


PHP
= 0; $i--)
    {
        // if the element can directly
        // jump to the end
        if ($arr[$i] >= $n - $i - 1)
            $count_jump[$i]++;
  
        // add the count of all the elements
        // that can reach to end and arr[i] 
        // can reach to them
        for ($j = $i + 1; $j < $n - 1 &&
             $j <= $arr[$i] + $i; $j++)
  
            // if element can reach to end then 
            // add its count to count_jump[i]
            if ($count_jump[$j] != -1)
                $count_jump[$i] += $count_jump[$j];
  
        // if arr[i] cannot reach to the end
        if ($count_jump[$i] == 0)
            $count_jump[$i] = -1;
    }
  
    // print count_jump for each
    // array element
    for ($i = 0; $i < $n; $i++)
        echo $count_jump[$i] . " ";
}
  
// Driver Code
$arr = array(1, 3, 5, 8, 9, 1,
                0, 7, 6, 8, 9);
$n = count($arr);
countWaysToJump($arr, $n);
  
// This code is contributed by Rajput-Ji
?>


输出:

52 52 28 16 8 -1 -1 4 2 1 0

时间复杂度:在最坏的情况下为O(n 2 )。