File tree 5 files changed +127
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings
implement-trie-prefix-tree
5 files changed +127
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Big-O ์์ธก
2
+ # Time : O(n)
3
+ # Space : O(1)
4
+ class Solution :
5
+ def maxProfit (self , prices : List [int ]) -> int :
6
+ min_price = prices [0 ]
7
+ profit = 0
8
+
9
+ for price in prices [1 :]:
10
+ if min_price > price :
11
+ min_price = price
12
+
13
+ profit = max (profit , price - min_price )
14
+ return profit
15
+
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ธก
2
+ # Time : O(n)
3
+ # Space : O(1)
4
+ class Codec :
5
+ def encode (self , strs : List [str ]) -> str :
6
+ """Encodes a list of strings to a single string.
7
+ """
8
+ new_str = "-!@$@#!_" .join (strs )
9
+ return new_str
10
+
11
+ def decode (self , s : str ) -> List [str ]:
12
+ """Decodes a single string to a list of strings.
13
+ """
14
+ return s .split ("-!@$@#!_" )
15
+
16
+
17
+
18
+ strs = ["Hello" ,"World" ]
19
+ codec = Codec ()
20
+ codec .decode (codec .encode (strs ))
21
+
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ธก
2
+ # Time : O(k * nlog(n)) (k : strs ๋ฐฐ์ด ์, n : strs ์์์ unique ๋จ์ด ๊ฐ์)
3
+ # Space : O(k + n)
4
+ class Solution :
5
+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
6
+ count_dict = {}
7
+ num = 0
8
+ dicted_list = []
9
+ for s in strs :
10
+ cur = str (sorted (Counter (s ).items ()))
11
+ dicted_list .append (cur )
12
+ if cur in count_dict :
13
+ continue
14
+ count_dict [cur ] = num
15
+ num += 1
16
+
17
+ ans_list = [[] for _ in range (len (count_dict ))]
18
+ for k , v in count_dict .items ():
19
+ for i , dicted in enumerate (dicted_list ):
20
+ if dicted == k :
21
+ ans_list [v ].append (strs [i ])
22
+ return ans_list
23
+
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ธก
2
+ # Time : O(n) (n : ๋จ์ด ๊ฐ์)
3
+ # Space : O(n)
4
+ class Trie :
5
+
6
+ def __init__ (self ):
7
+ self .trie_dict = {}
8
+ self .trie_dict_list = [{}]
9
+
10
+ def insert (self , word : str ) -> None :
11
+ self .trie_dict [word ] = 1
12
+ for i in range (1 , len (word ) + 1 ):
13
+ if i > len (self .trie_dict_list ) and i > 1 :
14
+ self .trie_dict_list .append ({})
15
+ self .trie_dict_list [i - 1 ][word [:i ]] = 1
16
+
17
+ def search (self , word : str ) -> bool :
18
+ return word in self .trie_dict
19
+
20
+ def startsWith (self , prefix : str ) -> bool :
21
+ if len (prefix ) > len (self .trie_dict_list ):
22
+ return False
23
+ return prefix in self .trie_dict_list [len (prefix )- 1 ]
24
+
25
+
26
+ # Your Trie object will be instantiated and called as such:
27
+ trie = Trie ()
28
+ trie .insert ("apple" )
29
+ trie .search ("apple" )
30
+ trie .search ("app" )
31
+ trie .startsWith ("app" )
32
+ trie .insert ("app" )
33
+ trie .search ("app" )
34
+
35
+ # param_2 = trie.search(word)
36
+ # param_3 = trie.startsWith(prefix)
37
+
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ธก
2
+ # Time : O(n^2) ์ต์
์ ๊ฒฝ์ฐ wordDict์ ๊ฐ์์ ์ ๊ณฑ๋งํผ
3
+ # Space : O(n^2) ์ต์
์ ๊ฒฝ์ฐ a์ wordDict์ ๊ฐ์์ ์ ๊ณฑ๋งํผ
4
+
5
+ # ์ฝ๊ฒ ์ ๊ทผํ ํ์ด
6
+ # ๋งจ ์์ ๊ฒ์ ์ฒ๋ฆฌํ ์ ์๋ ๊ฒ์ ๋ค ๊ณ ๋ฅด๊ธฐ (startswith)
7
+ # ๊ทธ๋ค์ ํ์ ๋์ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ง๊ณ ๋ค์ ๊ฒ์ผ๋ก ๋์ผํ๊ฒ ๋ฐ๋ณต ์งํํ๊ธฐ.
8
+
9
+ class Solution :
10
+ def wordBreak (self , s : str , wordDict : List [str ]) -> bool :
11
+ a = [[s ]]
12
+ i = 1
13
+ s_list = None
14
+ duplicate_dict = {}
15
+ while True :
16
+ s_list = a [- 1 ]
17
+ a .append ([])
18
+ for s in s_list :
19
+ for word in wordDict :
20
+ if s .startswith (word ):
21
+ surplus_word = s [len (word ):]
22
+ if surplus_word not in duplicate_dict :
23
+ a [- 1 ].append (surplus_word )
24
+ duplicate_dict [surplus_word ] = 1
25
+ # a[-1] = list(set(a[-1]))
26
+ if "" in a [- 1 ]:
27
+ return True
28
+ if not a [- 1 ]:
29
+ return False
30
+ i += 1
31
+
You canโt perform that action at this time.
0 commit comments