Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e723a5a

Browse files
Added new Solutions
1 parent a63d1b6 commit e723a5a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

‎coding_solutions/Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ This repository contains my answers to all Leetcode/HackerRank algorithm questio
88
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/TwoSum.py) |
99
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/AddTwoNum.py) |
1010
| 3 | [Search In Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/SearchInRotatedSortedArray.py) |
11+
| 4 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/MergeSorted.py) |
12+
| 5 | [Search In Rotated Sorted Array](https://leetcode.com/problems/first-bad-version/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/FirstBadVersion.py) |
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
Climbing Staris:
3+
You are climbing a staircase. It takes n steps to reach the top.
4+
5+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
6+
7+
Example 1:
8+
9+
Input: n = 2
10+
Output: 2
11+
Explanation: There are two ways to climb to the top.
12+
1. 1 step + 1 step
13+
2. 2 steps
14+
Example 2:
15+
16+
Input: n = 3
17+
Output: 3
18+
Explanation: There are three ways to climb to the top.
19+
1. 1 step + 1 step + 1 step
20+
2. 1 step + 2 steps
21+
3. 2 steps + 1 step
22+
'''
23+
24+
# Brute Force Solution
25+
class Solution:
26+
def climbStairs(self, n: int) -> int:
27+
def countClimbStairs(i, n):
28+
if i > n:
29+
return 0
30+
if i == n:
31+
return 1
32+
33+
return countClimbStairs(i+1, n) + countClimbStairs(i+2, n)
34+
35+
return countClimbStairs(0, n)
36+
37+
class Solution:
38+
def climbStairs(self, n: int) -> int:
39+
lPoint, rPoint = 0, 1
40+
for i in range(n+1):
41+
temp = lPoint
42+
lPoint = lPoint + rPoint
43+
rPoint = temp
44+
return lPoint
45+
46+
47+
class Solution:
48+
def climbStairs(self, n: int) -> int:
49+
c1 = 1
50+
c2 = 1
51+
for i in range(n-1):
52+
next = c1 + c2
53+
c1= c2
54+
c2 = next
55+
return c2
56+
57+
58+
59+
60+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /