File tree Expand file tree Collapse file tree 5 files changed +155
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings
implement-trie-prefix-tree Expand file tree Collapse file tree 5 files changed +155
-0
lines changed Original file line number Diff line number Diff line change
1
+ # time complexity : O(n)
2
+ # space complexity : O(1)
3
+ from typing import List
4
+
5
+
6
+ class Solution :
7
+ def maxProfit (self , prices : List [int ]) -> int :
8
+ n = len (prices )
9
+ min_price = prices [0 ]
10
+ max_profit = 0
11
+
12
+ for i in range (1 , n ):
13
+ min_price = min (min_price , prices [i ])
14
+ max_profit = max (max_profit , prices [i ] - min_price )
15
+
16
+ return max_profit
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ """
3
+ @param: strs: a list of strings
4
+ @return: encodes a list of strings to a single string.
5
+ """
6
+
7
+ def encode (self , strs ):
8
+ # write your code here
9
+ result = ""
10
+ for s in strs :
11
+ result += str (len (s )) + "@" + s
12
+ return result
13
+
14
+ """
15
+ @param: str: A string
16
+ @return: decodes a single string to a list of strings
17
+ """
18
+
19
+ def decode (self , str ):
20
+ # write your code here
21
+ result = []
22
+ i = 0
23
+ while i < len (str ):
24
+ j = i
25
+ # 시작점 아닌 경우
26
+ while str [j ] != "@" :
27
+ j += 1
28
+ # 시작점인 경우
29
+ length = int (str [i :j ])
30
+ word = str [j + 1 : j + 1 + length ]
31
+ result .append (word )
32
+ i = j + 1 + length
33
+ return result
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+ from collections import Counter , defaultdict
3
+
4
+ # class Solution:
5
+ # def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
6
+ #
7
+ # dict_anagrams = defaultdict(list)
8
+ # for idx, word in enumerate(strs):
9
+ # key = tuple(sorted(Counter(word).items()))
10
+ # dict_anagrams[key].append(word)
11
+ #
12
+ # return list(dict_anagrams.values())
13
+ class Solution :
14
+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
15
+
16
+ dict_anagrams = defaultdict (list )
17
+ for word in strs :
18
+ key = "" .join (sorted (word ))
19
+ dict_anagrams [key ].append (word )
20
+
21
+ return list (dict_anagrams .values ())
Original file line number Diff line number Diff line change
1
+ class TrieNode :
2
+ def __init__ (self ):
3
+ self .isEnd = False
4
+ self .links = {}
5
+
6
+
7
+ class Trie :
8
+
9
+ def __init__ (self ):
10
+ self ._root = TrieNode ()
11
+
12
+ def _recurAdd (self , node : TrieNode , word : str ) -> None :
13
+ if not word :
14
+ node .isEnd = True
15
+ return
16
+
17
+ ch = word [0 ]
18
+ # 부모 노드의 자식에 있는지 확인
19
+ next_link = node .links .get (ch )
20
+
21
+ if not next_link :
22
+ node .links [ch ] = TrieNode ()
23
+ next_link = node .links [ch ]
24
+
25
+ self ._recurAdd (next_link , word [1 :])
26
+
27
+ def insert (self , word : str ) -> None :
28
+ if not word :
29
+ return
30
+
31
+ self ._recurAdd (self ._root , word )
32
+
33
+ def _recurSearch (self , node : TrieNode , word : str ) -> bool :
34
+ if not word :
35
+ return node .isEnd
36
+
37
+ ch = word [0 ]
38
+ next_link = node .links .get (ch )
39
+ if next_link :
40
+ return self ._recurSearch (next_link , word [1 :])
41
+ return False
42
+
43
+ def search (self , word : str ) -> bool :
44
+ if not word :
45
+ return False
46
+
47
+ return self ._recurSearch (self ._root , word )
48
+
49
+ def startsWith (self , prefix : str ) -> bool :
50
+ node = self ._root
51
+ for ch in prefix :
52
+ if ch not in node .links :
53
+ return False
54
+ node = node .links [ch ]
55
+ return True
56
+
57
+ # Your Trie object will be instantiated and called as such:
58
+ # obj = Trie()
59
+ # obj.insert(word)
60
+ # param_2 = obj.search(word)
61
+ # param_3 = obj.startsWith(prefix)
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+
4
+ class Solution :
5
+ def wordBreak (self , s : str , wordDict : List [str ]) -> bool :
6
+ def dfs (subs : str ):
7
+ if subs in memo :
8
+ return memo [subs ]
9
+
10
+ if not subs :
11
+ return True
12
+
13
+ for word in wordDict :
14
+ if subs .startswith (word ):
15
+ if dfs (subs [len (word ):]):
16
+ memo [subs ] = True
17
+ return True
18
+
19
+ memo [subs ] = False
20
+ return False
21
+
22
+ memo = {}
23
+ return dfs (s )
24
+
You can’t perform that action at this time.
0 commit comments