File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# idea: DP
2- # 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.
3- # If you have any suggestions for how I can come up with DP, I would appreciate your comments :)
2+ # Time Complexity : O(n)
3+ '''
4+ n = 4
5+ -------------------------------
6+ (dp[3] + 1step)
7+ 1step + 1step + 1step + 1step
8+ 1step + 2step + 1step
9+ 2step + 1step + 1step
10+
11+ (dp[2] + 2step)
12+ 1step + 1step + 2step
13+ 2step + 2step
14+
15+ ================================
16+ n = 5
17+ --------------------------------
18+ (dp[4] + 1step)
19+ 1step + 1step + 1step + 1step + 1step
20+ 1step + 2step + 1step + 1step
21+ 1step + 1step + 2step + 1step
22+ 2step + 1step + 1step + 1step
23+ 2step + 2step + 1step
24+
25+ (dp[3] + 2step)
26+ 1step + 2step + 2step
27+ 2step + 1step + 2step
28+ 1step + 1step + 1step + 2step
29+ '''
30+
431
532class Solution :
633 def climbStairs (self , n : int ) -> int :
@@ -9,7 +36,6 @@ def climbStairs(self, n: int) -> int:
936 dp = [0 ] * (n + 1 )
1037 dp [2 ], dp [3 ] = 2 , 3
1138
12- #for i in range(4, n): error when n=4
1339 for i in range (4 , n + 1 ):
1440 dp [i ] = dp [i - 1 ] + dp [i - 2 ]
1541
Original file line number Diff line number Diff line change 1- #idea: dictionary
21from collections import Counter
2+ # class Solution:
3+ # def isAnagram(self, s: str, t: str) -> bool:
4+ # s_dic = Counter(sorted(s))
5+ # t_dic = Counter(sorted(t))
6+ # return s_dic==t_dic
37
4- class Solution :
5- def isAnagram (self , s : str , t : str ) -> bool :
6- s_dic = Counter (sorted (s ))
7- t_dic = Counter (sorted (t ))
8- print (s_dic , t_dic )
9- return s_dic == t_dic
8+ # fixed
9+ # do not need to both Counter and sorting
1010
11-
12-
13- # Trial and Error
14- '''
15- When you call sorted() on a dictionary, it only extracts and sorts the keys,and the values are completely ignored.
11+ # Ans 1 (Only Counter)
12+ # Time Complexity: O(N)
1613class Solution :
1714 def isAnagram (self , s : str , t : str ) -> bool :
1815 s_dic = Counter (s )
1916 t_dic = Counter (t )
20- print(s_dic, t_dic)
21- return sorted(s_dic)==sorted(t_dic)
22- '''
23-
24-
25-
17+ return s_dic == t_dic
2618
19+ # Ans 2 (Only Sorting)
20+ # Time Complexity: O(Nlog(N))
21+ class Solution :
22+ def isAnagram (self , s : str , t : str ) -> bool :
23+ s_arr = sorted (s )
24+ t_arr = sorted (t )
25+ return s_arr == t_arr
2726
You can’t perform that action at this time.
0 commit comments