给定N个楼梯,一个站在底部的人想到达顶部。他可以从给定的正整数数组arr []爬任意数量的台阶。任务是找到到达顶部的所有可能方法的计数。
例子:
Input: arr[] = {1, 3, 5}, N = 5
Output: 5
(0 -> 1 -> 2 -> 3 -> 4 -> 5), (0 -> 1 -> 2 -> 5),
(0 -> 1 -> 4 -> 5), (0 -> 3 -> 4 -> 5)
and (0 -> 5) are the only possible ways.
Input: arr[] = {1, 2, 3}, N = 10
Output: 274
天真的方法:使用递归,找到所有可能的方法并计数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Recursive function to return the
// total ways to reach the n'th stair
int countWays(int n, int arr[], int len)
{
// Base case
if (n == 0)
return 1;
// To store the number of ways
int no_ways = 0;
// Iterate each element of the given array
for (int i = 0; i < len; i++) {
// Here consider only the values of
// "n - arr[i]" >= 0 because negative values
// of n in the stair function are
// not defined
if (n - arr[i] >= 0) {
no_ways += countWays(n - arr[i], arr, len);
}
}
return no_ways;
}
// Driver code
int main()
{
int arr[] = { 1, 3, 5 };
int len = sizeof(arr) / sizeof(int);
int n = 5;
cout << countWays(n, arr, len);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Recursive function to return the
// total ways to reach the n'th stair
static int countWays(int n, int arr[], int len)
{
// Base case
if (n == 0)
return 1;
// To store the number of ways
int no_ways = 0;
// Iterate each element of the given array
for (int i = 0; i < len; i++) {
// Here consider only the values of
// "n - arr[i]" >= 0 because negative values
// of n in the stair function are
// not defined
if (n - arr[i] >= 0) {
no_ways += countWays(n - arr[i], arr, len);
}
}
return no_ways;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 3, 5 };
int len = arr.length;
;
int n = 5;
System.out.println(countWays(n, arr, len));
}
}
Python
# Python3 implementation of the approach
# Recursive function to return the
# total ways to reach the n'th stair
def countWays(n, arr):
# Base case
if (n == 0):
return 1
# To store the number of ways
no_ways = 0
# Iterate each element of the given array
for i in arr:
# Here consider only the values of
# "n - arr[i]" >= 0 because negative values
# of n in the stair function are
# not defined
if (n - i >= 0):
no_ways = no_ways + countWays(n - i, arr)
return no_ways
# Driver code
arr = [1, 3, 5]
n = 5
print(countWays(n, arr))
C#
// C# implementation of the approach
using System;
class GFG
{
// Recursive function to return the
// total ways to reach the n'th stair
static int countWays(int n, int []arr,
int len)
{
// Base case
if (n == 0)
return 1;
// To store the number of ways
int no_ways = 0;
// Iterate each element
// of the given array
for (int i = 0; i < len; i++)
{
// Here consider only the values of
// "n - arr[i]" >= 0 because negative values
// of n in the stair function are
// not defined
if (n - arr[i] >= 0)
{
no_ways += countWays(n - arr[i], arr, len);
}
}
return no_ways;
}
// Driver code
public static void Main()
{
int []arr = { 1, 3, 5 };
int len = arr.Length;
int n = 5;
Console.Write(countWays(n, arr, len));
}
}
// This code is contributed by Mohit Kumar
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the total number
// of ways to reach the n'th stair
int countWays(int n, int arr[], int len)
{
// To store the number of ways
// to reach the ith stair
int count[n + 1] = { 0 };
count[0] = 1;
// Base case
if (n == 0)
return 1;
// For every stair
for (int i = 1; i <= n; i++) {
// To store the count of ways
// till the current stair
int no_ways = 0;
// Every step from the array
for (int j = 0; j < len; j++) {
// Here consider only
// the values of "i - arr[j]" >= 0
// because negative values
// indicates reverse climbing
// of steps
if (i - arr[j] >= 0) {
no_ways += count[i - arr[j]];
}
count[i] = no_ways;
}
}
return count[n];
}
// Driver code
int main()
{
int arr[] = { 1, 3, 5 };
int len = sizeof(arr) / sizeof(int);
int n = 5;
cout << countWays(n, arr, len);
return 0;
}
Java
// java implementation of the approach
class GFG {
// Function to return the total number
// of ways to reach the n'th stair
static int countWays(int n, int arr[], int len)
{
// To store the number of ways
// to reach the ith stair
int count[] = new int[n + 1];
count[0] = 1;
// Base case
if (n == 0)
return 1;
// For every stair
for (int i = 1; i <= n; i++) {
// To store the count of ways
// till the current stair
int no_ways = 0;
// Every step from the array
for (int j = 0; j < len; j++) {
// Here consider only
// the values of "i - arr[j]" >= 0
// because negative values
// indicates reverse climbing
// of steps
if (i - arr[j] >= 0) {
no_ways += count[i - arr[j]];
}
count[i] = no_ways;
}
}
return count[n];
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 3, 5 };
int len = arr.length;
int n = 5;
System.out.print(countWays(n, arr, len));
}
}
Python3
# Python3 implementation of the approach
# Function to return the total number
# of ways to reach the n'th stair
def countWays(n, arr):
# To store the number of ways
# to reach the ith stair
count = [0] * (n + 1)
count[0] = 1
# Base case
if (n == 0):
return 1
# For every stair
for i in range(1, n + 1):
# To store the count of ways
# till the current stair
no_ways = 0
# Every step from the array
for j in arr:
# Here consider only
# the values of "i - arr[j]" >= 0
# because negative values
# indicates reverse climbing
# of steps
if (i - j >= 0):
no_ways += count[i - j]
count[i] = no_ways
return count[n]
# Driver code
arr = [1, 3, 5]
n = 5
print(countWays(n, arr))
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the total number
// of ways to reach the n'th stair
static int countWays(int n, int []arr, int len)
{
// To store the number of ways
// to reach the ith stair
int []count = new int[n + 1];
count[0] = 1;
// Base case
if (n == 0)
return 1;
// For every stair
for (int i = 1; i <= n; i++)
{
// To store the count of ways
// till the current stair
int no_ways = 0;
// Every step from the array
for (int j = 0; j < len; j++)
{
// Here consider only
// the values of "i - arr[j]" >= 0
// because negative values
// indicates reverse climbing
// of steps
if (i - arr[j] >= 0)
{
no_ways += count[i - arr[j]];
}
count[i] = no_ways;
}
}
return count[n];
}
// Driver code
public static void Main()
{
int []arr = { 1, 3, 5 };
int len = arr.Length;
int n = 5;
Console.WriteLine(countWays(n, arr, len));
}
}
// This code is contributed by AnkitRai01
输出:
5
高效的方法:在这种方法中,不使用递归方法,而是使用基于动态编程的方法。
- 创建一个数组count [] ,其大小等于步骤总数+ 1,并将所有元素初始化为0,然后将第一个元素(即count [0])初始化为1。
- 在for循环内初始化变量no_ways = 0 ,每次从0开始初始化新的爬楼梯方式。
- 仅当i – x [j]≥0时才将count [i – x [j]]添加到no_ways 。
- 最后,返回count [N]本质上是攀登第N个楼梯的方式数量。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the total number
// of ways to reach the n'th stair
int countWays(int n, int arr[], int len)
{
// To store the number of ways
// to reach the ith stair
int count[n + 1] = { 0 };
count[0] = 1;
// Base case
if (n == 0)
return 1;
// For every stair
for (int i = 1; i <= n; i++) {
// To store the count of ways
// till the current stair
int no_ways = 0;
// Every step from the array
for (int j = 0; j < len; j++) {
// Here consider only
// the values of "i - arr[j]" >= 0
// because negative values
// indicates reverse climbing
// of steps
if (i - arr[j] >= 0) {
no_ways += count[i - arr[j]];
}
count[i] = no_ways;
}
}
return count[n];
}
// Driver code
int main()
{
int arr[] = { 1, 3, 5 };
int len = sizeof(arr) / sizeof(int);
int n = 5;
cout << countWays(n, arr, len);
return 0;
}
Java
// java implementation of the approach
class GFG {
// Function to return the total number
// of ways to reach the n'th stair
static int countWays(int n, int arr[], int len)
{
// To store the number of ways
// to reach the ith stair
int count[] = new int[n + 1];
count[0] = 1;
// Base case
if (n == 0)
return 1;
// For every stair
for (int i = 1; i <= n; i++) {
// To store the count of ways
// till the current stair
int no_ways = 0;
// Every step from the array
for (int j = 0; j < len; j++) {
// Here consider only
// the values of "i - arr[j]" >= 0
// because negative values
// indicates reverse climbing
// of steps
if (i - arr[j] >= 0) {
no_ways += count[i - arr[j]];
}
count[i] = no_ways;
}
}
return count[n];
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 3, 5 };
int len = arr.length;
int n = 5;
System.out.print(countWays(n, arr, len));
}
}
Python3
# Python3 implementation of the approach
# Function to return the total number
# of ways to reach the n'th stair
def countWays(n, arr):
# To store the number of ways
# to reach the ith stair
count = [0] * (n + 1)
count[0] = 1
# Base case
if (n == 0):
return 1
# For every stair
for i in range(1, n + 1):
# To store the count of ways
# till the current stair
no_ways = 0
# Every step from the array
for j in arr:
# Here consider only
# the values of "i - arr[j]" >= 0
# because negative values
# indicates reverse climbing
# of steps
if (i - j >= 0):
no_ways += count[i - j]
count[i] = no_ways
return count[n]
# Driver code
arr = [1, 3, 5]
n = 5
print(countWays(n, arr))
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the total number
// of ways to reach the n'th stair
static int countWays(int n, int []arr, int len)
{
// To store the number of ways
// to reach the ith stair
int []count = new int[n + 1];
count[0] = 1;
// Base case
if (n == 0)
return 1;
// For every stair
for (int i = 1; i <= n; i++)
{
// To store the count of ways
// till the current stair
int no_ways = 0;
// Every step from the array
for (int j = 0; j < len; j++)
{
// Here consider only
// the values of "i - arr[j]" >= 0
// because negative values
// indicates reverse climbing
// of steps
if (i - arr[j] >= 0)
{
no_ways += count[i - arr[j]];
}
count[i] = no_ways;
}
}
return count[n];
}
// Driver code
public static void Main()
{
int []arr = { 1, 3, 5 };
int len = arr.Length;
int n = 5;
Console.WriteLine(countWays(n, arr, len));
}
}
// This code is contributed by AnkitRai01
输出:
5
时间复杂度: O(N * len)其中N是阶梯数,len是数组的长度。