diff --git a/climbing-stairs/prograsshopper.py b/climbing-stairs/prograsshopper.py new file mode 100644 index 000000000..da528bda2 --- /dev/null +++ b/climbing-stairs/prograsshopper.py @@ -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 diff --git a/valid-anagram/prograsshopper.py b/valid-anagram/prograsshopper.py new file mode 100644 index 000000000..87b6bf2d8 --- /dev/null +++ b/valid-anagram/prograsshopper.py @@ -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())) + +