Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions climbing-stairs/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def climbStairs(self, n: int) -> int:
'''
sol 1
Runtime: Beats 100.00% / O(n^2)
Memory: Beats 8.16% / O(1)
'''
import math
ways = 1
max_count_2steps = int(n/2)
for i in range(1, max_count_2steps+1):
current = math.perm(n - i) / (math.perm(i) * math.perm(n-(2*i)))
ways += current
return int(ways)

'''
sol 2
Runtime: Beats 100.00% / O(n^2)
Memory: Beats 48.54% / O(1)
'''
ways = 0
max_count_2steps = n // 2
for i in range(max_count_2steps + 1):
ways += math.comb(n - i, i)
return ways
Comment on lines +16 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

루프가 i=0부터 n/2까지 한 번만 돌기 때문에 O(n^2)보다는 O(n)에 가까울 것 같습니다!

35 changes: 35 additions & 0 deletions valid-anagram/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
'''
sol 1
Runtime: Beats 78.74% / O(N)
Memory: Beats 64.30% / O(1)
'''
s_dict = dict()
for elem in s:
try:
s_dict[elem] += 1
except Exception as ex:
s_dict[elem] = 1
for elem in t:
try:
s_dict[elem] -= 1
except Exception as ex:
return False
return not (any(s_dict.values()))

'''
sol 2
Runtime: Beats 72.12% / O(N)
Memory: Beats 97.51% / O(1)
'''
from itertools import zip_longest
from collections import defaultdict

ana_dict = defaultdict(int)
for elem1, elem2 in zip_longest(s, t):
ana_dict[elem1] += 1
ana_dict[elem2] -= 1
return not (any(ana_dict.values()))