-
Notifications
You must be signed in to change notification settings - Fork 0
/
interviewbit.py
80 lines (53 loc) · 1.53 KB
/
interviewbit.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Problem Statement(Interviewee)
# Stairs
# You are climbing a stair case. It takes n steps to reach to the top.
# Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
# Example :
# Input : 3
# Return : 3
# Steps : [1 1 1], [1 2], [2 1]
class Solution:
# @param A : integer
# @return an integer
def climbStairs(self, A):
if A == 0 or A == 1:
return 1
return self.climbStairs(A-1) + self.climbStairs(A-2)
def recursive(A, counter):
if A == 0:
return counter+=1
else:
if A%2 == 0:
return recursive(A-2,counter)
else:
return recursive(A-1,counter)
return recursive(A,0)
class Solution {
public:
int climbStairs(int n) {
int ways[n+1];
ways[0] = 1;
ways[1] = 1;
for (int i = 2; i <= n; i++) ways[i] = ways[i - 1] + ways[i - 2];
return ways[n];
}
};
class Solution:
# @param A : integer
# @return an integer
def climbStairs(self, A):
DP = [1]*(A+1)
for i in range(2, A+1):
DP[i] = DP[i-1] + DP[i-2]
return DP[A]
'''
f(n) = f(n-1) + f(n-2)
F(4) = F(3) + F(2)
fib(n) = f(n-1) + f(n-2)
'''
# 4:
# 1 1 1 1
# 2 2
# 1 2 1
# 2 1 1
# 1 1 2