给定整数N个楼梯,任务是通过走 1 或 2 步任意次数但只走 3 步,计算到达第N个楼梯的方式数。
例子:
Input: N = 4
Output: 2
Explanation:
Since a step of 3 has to be taken compulsorily and only once. So, there are only two possible ways: (1, 3) or (3, 1)
Input: N = 5
Output: 5
Explanation:
There are five possible ways to reach 5th stair: (2, 3), (3, 2), (1, 1, 3), (1, 3, 1), (3, 1, 1)
方法:这个问题可以使用排列和组合的概念来解决。有一个约束,根据该约束,一次必须执行 3 个步骤。这个想法是计算可能采取 3 个步骤的位置数量。
现在,下一个任务是找到移动到第N个楼梯的可能的两步数,即 (N – 3) / 2。这样,如果有两步,则所有可能的路径都将被覆盖—— step 每次递增一次,对于每一步,都会计算所有可能的组合。
在概括步骤之后,每个可能序列的可能组合将是
Ways = (Length of sequence)! * (Count of 2-step)!
------------------------------------------
(Count of 1-step)!
算法:
- 将序列的长度初始化为 (N – 2)。
- 将 1 步的计数初始化为 (N – 3)。
- 将 2-steps 的计数初始化为 0
- 将 2 步的可能计数初始化为 (N-3)/2。
- 运行一个循环,直到 2 步的可能计数等于 2 步的实际计数。
- 借助序列的当前长度、1 步数、2 步数和上面给出的公式,找到可能的方法数。
- 将序列的长度减少 1。
- 将 2-steps 的数量增加 1
下面是上述方法的实现。
C++
// C++ implementation to find the number
// the number of ways to reach Nth stair
// by taking 1 or 2 steps at a time and
// 3rd step exactly once
#include
using namespace std;
int factorial(int n)
{
// Single line to find factorial
return (n == 1 || n == 0) ? 1 :
n * factorial(n - 1);
}
// Function to find
// the number of ways
int ways(int n)
{
// Base Case
if (n < 3)
{
return 0;
}
// Count of 2-steps
int c2 = 0;
// Count of 1-steps
int c1 = n - 3;
// Initial length of sequence
int l = c1 + 1;
int s = 0;
// Expected count of 2-steps
int exp_c2 = c1 / 2;
// Loop to find the ways for
// every possible sequence
while (exp_c2 >= c2)
{
int f1 = factorial(l);
int f2 = factorial(c1);
int f3 = factorial(c2);
int f4 = (f2 * f3);
s += f1 / f4;
c2 += 1;
c1 -= 2;
l -= 1;
}
return s;
}
// Driver code
int main()
{
int n = 7;
int ans = ways(n);
cout << ans << endl;
return 0;
}
// This code is contributed by coder001
Java
// Java implementation to find the number
// the number of ways to reach Nth stair
// by taking 1 or 2 steps at a time and
// 3rd step exactly once
class GFG {
static int factorial(int n)
{
// Single line to find factorial
return (n == 1 || n == 0) ? 1 :
n * factorial(n - 1);
}
// Function to find
// the number of ways
static int ways(int n)
{
// Base Case
if (n < 3)
{
return 0;
}
// Count of 2-steps
int c2 = 0;
// Count of 1-steps
int c1 = n - 3;
// Intial length of sequence
int l = c1 + 1;
int s = 0;
// Expected count of 2-steps
int exp_c2 = c1 / 2;
// Loop to find the ways for
// every possible sequence
while (exp_c2 >= c2)
{
int f1 = factorial(l);
int f2 = factorial(c1);
int f3 = factorial(c2);
int f4 = (f2 * f3);
s += f1 / f4;
c2 += 1;
c1 -= 2;
l -= 1;
}
return s;
}
// Driver Code
public static void main(String args[])
{
int n = 7;
int ans = ways(n);
System.out.println(ans);
}
}
// This code is contributed by rutvik_56
Python3
# Python3 implementation to find the number
# the number of ways to reach Nth stair
# by taking 1 or 2 steps at a time and
# 3rd Step exactly once
import math
# Function to find
# the number of ways
def ways(n):
# Base Case
if n < 3:
return 0
# Count of 2-steps
c2 = 0
# Count of 1-steps
c1 = n-3
# Intial length of sequence
l = c1 + 1
s = 0
# expected count of 2-steps
exp_c2 = c1 / 2
# Loop to find the ways for
# every possible sequence
while exp_c2 >= c2:
f1 = math.factorial(l)
f2 = math.factorial(c1)
f3 = math.factorial(c2)
s += f1//(f2 * f3)
c2 += 1
c1 -= 2
l -= 1
return s
# Driver Code
if __name__ == "__main__":
N = 7
print(ways(N))
C#
// C# implementation to find the number
// the number of ways to reach Nth stair
// by taking 1 or 2 steps at a time and
// 3rd step exactly once
using System;
class GFG{
static int factorial(int n)
{
// Single line to find factorial
return (n == 1 || n == 0) ? 1 :
n * factorial(n - 1);
}
// Function to find
// the number of ways
static int ways(int n)
{
// Base Case
if (n < 3)
{
return 0;
}
// Count of 2-steps
int c2 = 0;
// Count of 1-steps
int c1 = n - 3;
// Intial length of sequence
int l = c1 + 1;
int s = 0;
// Expected count of 2-steps
int exp_c2 = c1 / 2;
// Loop to find the ways for
// every possible sequence
while (exp_c2 >= c2)
{
int f1 = factorial(l);
int f2 = factorial(c1);
int f3 = factorial(c2);
int f4 = (f2 * f3);
s += f1 / f4;
c2 += 1;
c1 -= 2;
l -= 1;
}
return s;
}
// Driver code
static void Main()
{
int n = 7;
int ans = ways(n);
Console.WriteLine(ans);
}
}
// This code is contributed by divyeshrabadiya07
Javascript
输出:
20
性能分析:
- 时间复杂度:与上面的方法一样,有一个循环来检查每个序列的可能排列,这些排列可以上升到 (N-3),这需要 O(N) 时间,并找到可能需要 O(N) 的组合,因此时间复杂度将为O(N 2 ) 。
- 空间复杂度:与上述方法一样,没有使用额外的空间,因此空间复杂度为O(1) 。
注意:通过预先计算每个数字的阶乘直到 N,时间复杂度可以提高到 O(N)。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live