计算到达第 N 级楼梯的方法 |第 2 组
有N个楼梯,一个人站在底部想爬到顶部。此人一次可以爬1 个楼梯或2 个楼梯。数一数路数,人能登顶。
例子:
Input: N = 1
Output: 1
Explanation: There is only one way to climb 1st stair
Input: N= 2
Output: 2
Explanation: There are two ways and the sequence to climb to Nth stair are: (1, 2), (2)
Input: N = 4
Output: 5
Explanation: There are five possible ways and the sequence to climb to Nth stair are:
(1, 2, 3, 4), (1, 2, 4), (2, 3, 4), (1, 3, 4), (2, 4)
该问题的 Set-1 讨论了动态规划和滑动窗口以及其他几种方法。但是这种方式会有更好的整体辅助空间。
方法:这个问题可以根据以下观察以更好的空间复杂度来解决:
If observed closely, the value of current state ( i ) depends on two states: the value of the previous state (i – 1) and the value of (i – 2) state.
Instead of using extra space only two variables can be used to store the values of the above-mentioned two states.
As one can either climb one step or two step at a time, so ith state value is the summation of the values of (i – 1)th and (i – 2)th state.
请按照下面给出的插图更好地理解。
插图:
Initially, There are one ways to reach 1st stair and the sequence to climb to 1st stair is: (1)
There are two ways to reach 2nd stair and the sequence to climb to 2nd stair are: (1, 2), (2)
For 3rd stair:
=> Number of ways to reach 3rd stair = prev1 + prev2. i.e. curr = 2 + 1 = 3.
=> prev2 = prev1 = 2. prev1 = curr = 3
For 4th stair:
=> Number of ways to reach 3rd stair = prev1 + prev2. i.e. curr = 3 + 2 = 5.
=> prev2 = prev1 = 3. prev1 = curr = 5
For 5th stair:
=> Number of ways to reach 3rd stair = prev1 + prev2. i.e. curr = 5 + 3 = 8.
=> prev2 = prev1 = 5. prev1 = curr = 8
For 6th stair:
=> Number of ways to reach 3rd stair = prev1 + prev2. i.e. curr = 8 + 5 = 13.
=> prev2 = prev1 = 8. prev1 = curr = 13
Below is the image representation of each step of above explanation:
请按照以下步骤解决问题:
- 声明两个变量(例如prev1和prev2 )来存储任何当前状态的前两个状态,并用到达第一个和第二个楼梯的方式数来初始化它们。
- 从第 3个楼梯迭代到第 N 个楼梯:
- 如上述观察所示,计算到达当前楼梯的路径数。
- 现在根据观察更新prev1和prev2变量。
- 返回作为结果获得的第 N 个楼梯的值。
下面是上述方法的实现:
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to find minimum number of steps
int minSteps(int N)
{
// Can make 1 jump only
if (N == 1)
return 1;
// Can jump like {2} and {1 + 1}
// so two ways for n == 2;
if (N == 2)
return 2;
int curr;
int prev2 = 1;
int prev1 = 2;
// Iterate from 3rd stair to nth stair
for (int i = 3; i <= N; i++) {
curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
}
return curr;
}
// Driver code
int main()
{
int N = 6;
// Function call
cout << minSteps(N);
return 0;
}
Java
// Java code to implement the approach
import java.util.*;
class GFG {
// Function to find minimum number of steps
static int minSteps(int N)
{
// Can make 1 jump only
if (N == 1)
return 1;
// Can jump like {2} and {1 + 1}
// so two ways for n == 2;
if (N == 2)
return 2;
int curr = 0;
int prev2 = 1;
int prev1 = 2;
// Iterate from 3rd stair to nth stair
for (int i = 3; i <= N; i++) {
curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
}
return curr;
}
// Driver code
public static void main (String[] args) {
int N = 6;
// Function call
System.out.print(minSteps(N));
}
}
// This code is contributed by hrithikgarg03188.
Javascript
13
时间复杂度: O(N)
辅助空间:O(1)