Skip to content

Commit ff30ce3

Browse files
committed
Adding Simple Solution for Problem - 70 - Climbing Stairs
1 parent ce876a6 commit ff30ce3

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Climbing_Stairs/SimpleSolution.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 70
6+
## Problem Name: Climbing Stairs
7+
##===================================
8+
#You are climbing a stair case. It takes n steps to reach to the top.
9+
#
10+
#Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
11+
#
12+
#Note: Given n will be a positive integer.
13+
#
14+
#Example 1:
15+
#
16+
#Input: 2
17+
#Output: 2
18+
#Explanation: There are two ways to climb to the top.
19+
#1. 1 step + 1 step
20+
#2. 2 steps
21+
#Example 2:
22+
#
23+
#Input: 3
24+
#Output: 3
25+
#Explanation: There are three ways to climb to the top.
26+
#1. 1 step + 1 step + 1 step
27+
#2. 1 step + 2 steps
28+
#3. 2 steps + 1 step
29+
class Solution:
30+
def climbStairs(self, n):
31+
if n == 1: #Condition-check: If n is 1
32+
return 1 #We returns 1 step
33+
if n == 2: #Condition-check: If n is 2
34+
return 2 #We returns 2 step
35+
tmpA = 1 #Initialize tmpA
36+
tmpB = 2 #Initialize tmpB
37+
for i in range(3, n + 1): #Loop through 3 to n+1
38+
tmp = tmpA + tmpB #Initialize tmp and update it
39+
tmpA, tmpB = tmpB, tmpA #Update tmpA and tmpB
40+
return tmpB #Return tmpB at the end.

0 commit comments

Comments
 (0)