-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
venv | ||
venv | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |