diff --git a/climbing-stairs/ppxyn1.py b/climbing-stairs/ppxyn1.py index 7515d102c4..063fff2491 100644 --- a/climbing-stairs/ppxyn1.py +++ b/climbing-stairs/ppxyn1.py @@ -1,6 +1,33 @@ # idea: DP -# I'm not always sure how to approach DP problems. I just try working through a few examples step by step and then check that it would be DP. -# If you have any suggestions for how I can come up with DP, I would appreciate your comments :) +# Time Complexity : O(n) +''' +n = 4 +------------------------------- +(dp[3] + 1step) +1step + 1step + 1step + 1step +1step + 2step + 1step +2step + 1step + 1step + +(dp[2] + 2step) +1step + 1step + 2step +2step + 2step + +================================ +n = 5 +-------------------------------- +(dp[4] + 1step) +1step + 1step + 1step + 1step + 1step +1step + 2step + 1step + 1step +1step + 1step + 2step + 1step +2step + 1step + 1step + 1step +2step + 2step + 1step + +(dp[3] + 2step) +1step + 2step + 2step +2step + 1step + 2step +1step + 1step + 1step + 2step +''' + class Solution: def climbStairs(self, n: int) -> int: @@ -9,7 +36,6 @@ def climbStairs(self, n: int) -> int: dp = [0] * (n+1) dp[2], dp[3] = 2, 3 - #for i in range(4, n): error when n=4 for i in range(4, n+1): dp[i] = dp[i-1] + dp[i-2] diff --git a/valid-anagram/ppxyn1.py b/valid-anagram/ppxyn1.py index 77ab26595a..68fc32cc47 100644 --- a/valid-anagram/ppxyn1.py +++ b/valid-anagram/ppxyn1.py @@ -1,27 +1,26 @@ -#idea: dictionary from collections import Counter +# class Solution: +# def isAnagram(self, s: str, t: str) -> bool: +# s_dic = Counter(sorted(s)) +# t_dic = Counter(sorted(t)) +# return s_dic==t_dic -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - s_dic = Counter(sorted(s)) - t_dic = Counter(sorted(t)) - print(s_dic, t_dic) - return s_dic==t_dic +# fixed +# do not need to both Counter and sorting - - -# Trial and Error -''' -When you call sorted() on a dictionary, it only extracts and sorts the keys,and the values are completely ignored. +# Ans 1 (Only Counter) +# Time Complexity: O(N) class Solution: def isAnagram(self, s: str, t: str) -> bool: s_dic = Counter(s) t_dic = Counter(t) - print(s_dic, t_dic) - return sorted(s_dic)==sorted(t_dic) -''' - - - + return s_dic==t_dic +# Ans 2 (Only Sorting) +# Time Complexity: O(Nlog(N)) +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + s_arr = sorted(s) + t_arr = sorted(t) + return s_arr==t_arr