File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ # 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 :)
4+
5+ class Solution :
6+ def climbStairs (self , n : int ) -> int :
7+ if n <= 2 :
8+ return n
9+ dp = [0 ] * (n + 1 )
10+ dp [2 ], dp [3 ] = 2 , 3
11+
12+ #for i in range(4, n): error when n=4
13+ for i in range (4 , n + 1 ):
14+ dp [i ] = dp [i - 1 ] + dp [i - 2 ]
15+
16+ return dp [n ]
17+
18+
19+
20+
Original file line number Diff line number Diff line change 1+ #idea: dictionary
2+ from collections import Counter
3+
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
10+
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.
16+ class Solution:
17+ def isAnagram(self, s: str, t: str) -> bool:
18+ s_dic = Counter(s)
19+ t_dic = Counter(t)
20+ print(s_dic, t_dic)
21+ return sorted(s_dic)==sorted(t_dic)
22+ '''
23+
24+
25+
26+
27+
You can’t perform that action at this time.
0 commit comments