-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec5_nth.cpp
More file actions
39 lines (34 loc) · 714 Bytes
/
Copy pathlec5_nth.cpp
File metadata and controls
39 lines (34 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// https://leetcode.com/problems/climbing-stairs/description/
//with array
class Solution {
public:
int climbStairs(int n) {
int arr[n+1];
arr[0] = 1;
arr[1]=1;
for(int i = 2; i<=n ; i++)
{
arr[i] = arr[i-1]+arr[i-2];
}
return arr[n];
}
};
//without array
class Solution {
public:
int climbStairs(int n) {
if(n<=1)
{
return n;
}
int ways1 = 1 , ways2 = 1 ;//intialise
int totalways = 0;
for(int i = 2; i<=n;i++)
{
totalways = ways1 + ways2;
ways1 = ways2;
ways2 = totalways;
}
return totalways;
}
};