Skip to content

Commit 5bc1630

Browse files
Merge pull request #1773 from prograsshopper/main
[prograsshopper] Week 2 Solutions
2 parents 4c90350 + 87b6ad0 commit 5bc1630

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

climbing-stairs/prograsshopper.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
'''
4+
sol 1
5+
Runtime: Beats 100.00% / O(n^2)
6+
Memory: Beats 8.16% / O(1)
7+
'''
8+
import math
9+
ways = 1
10+
max_count_2steps = int(n/2)
11+
for i in range(1, max_count_2steps+1):
12+
current = math.perm(n - i) / (math.perm(i) * math.perm(n-(2*i)))
13+
ways += current
14+
return int(ways)
15+
16+
'''
17+
sol 2
18+
Runtime: Beats 100.00% / O(n^2)
19+
Memory: Beats 48.54% / O(1)
20+
'''
21+
ways = 0
22+
max_count_2steps = n // 2
23+
for i in range(max_count_2steps + 1):
24+
ways += math.comb(n - i, i)
25+
return ways

valid-anagram/prograsshopper.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
'''
4+
sol 1
5+
Runtime: Beats 78.74% / O(N)
6+
Memory: Beats 64.30% / O(1)
7+
'''
8+
s_dict = dict()
9+
for elem in s:
10+
try:
11+
s_dict[elem] += 1
12+
except Exception as ex:
13+
s_dict[elem] = 1
14+
for elem in t:
15+
try:
16+
s_dict[elem] -= 1
17+
except Exception as ex:
18+
return False
19+
return not (any(s_dict.values()))
20+
21+
'''
22+
sol 2
23+
Runtime: Beats 72.12% / O(N)
24+
Memory: Beats 97.51% / O(1)
25+
'''
26+
from itertools import zip_longest
27+
from collections import defaultdict
28+
29+
ana_dict = defaultdict(int)
30+
for elem1, elem2 in zip_longest(s, t):
31+
ana_dict[elem1] += 1
32+
ana_dict[elem2] -= 1
33+
return not (any(ana_dict.values()))
34+
35+

0 commit comments

Comments
 (0)