Skip to content

Commit

Permalink
easy 70 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
samjazaaa committed Sep 28, 2022
1 parent c905380 commit cdd6cb5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
venv
venv
__pycache__
28 changes: 28 additions & 0 deletions src/easy/70-climbing-stairs/stairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# https://leetcode.com/problems/climbing-stairs/

class Solution:
def climbStairs(self, n: int) -> int:
combos = 0
max_pairs = n // 2

for pairs in range(max_pairs + 1):
# trying combinations with i pairs of two
combos += self.combinations(pairs, n)

return combos

def combinations(self, pairs: int, positions: int) -> int:
# how many combinations to fit pairs pairs into positions?
if (pairs <= 0):
return 1

combos = 0

for i in range(positions - 1):
# start with pair and check all other combinations
combos += self.combinations(pairs-1, positions - (i+2))

return combos

# TODO exceeds timelimit since values get calculated recursively miltiple times
# => memoization?
53 changes: 53 additions & 0 deletions src/easy/70-climbing-stairs/test_stairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from stairs import Solution


class TestStairs:
def test_one(self):
# x
assert Solution().climbStairs(1) == 1

def test_two(self):
# x x
# xx
assert Solution().climbStairs(2) == 2

def test_three(self):
# x x x
# xx x
# x xx
assert Solution().climbStairs(3) == 3

def test_four(self):
# x x x x
# xx x x
# x xx x
# x x xx
# xx xx
assert Solution().climbStairs(4) == 5

def test_five(self):
# x x x x x
# xx x x x
# x xx x x
# x x xx x
# x x x xx
# xx xx x
# xx x xx
# x xx xx
assert Solution().climbStairs(5) == 8

def test_six(self):
# x x x x x x
# xx x x x x
# x xx x x x
# x x xx x x
# x x x xx x
# x x x x xx
# xx xx x x
# xx x xx x
# xx x x xx
# x xx xx x
# x xx x xx
# x x xx xx
# xx xx xx
assert Solution().climbStairs(6) == 13

0 comments on commit cdd6cb5

Please sign in to comment.