File tree Expand file tree Collapse file tree 4 files changed +141
-0
lines changed
best-time-to-buy-and-sell-stock
implement-trie-prefix-tree Expand file tree Collapse file tree 4 files changed +141
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def maxProfit (self , prices : List [int ]) -> int :
3
+ """
4
+ ๊ฐ์ฅ ์์ต์ ๋ง์ด ์ป์ ์ ์๋๋ก ์ ์ ์ ๋งค์, ๊ณ ์ ์ ๋งค๋
5
+ ๋งค์์ ๋งค๋๋ ์๋ก ๋ค๋ฅธ ๋
6
+
7
+ min_price๋ฅผ ๋นผ๊ฐ๋ฉด์ price ์
๋ฐ์ดํธ
8
+
9
+ Time Complexity : O(n)
10
+ Space Complexity : O(1)
11
+ """
12
+
13
+ min_price = max (prices )
14
+ days = len (prices )
15
+
16
+ for day in range (days ):
17
+ min_price = min (prices [day ], min_price )
18
+ prices [day ] -= min_price
19
+
20
+ return max (prices )
21
+
22
+
23
+
Original file line number Diff line number Diff line change
1
+ from collections import defaultdict
2
+
3
+ class Solution :
4
+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
5
+ # Naive Solution : Sort string
6
+
7
+ # ๊ฐ ๋จ์ด๋ง๋ค ๋ชจ๋ ์ ๋ ฌ์ ํ ๋ค ํด๋น ๊ฐ์ hash๋ก ์ฌ์ฉํ์ฌ ๋์
๋๋ฆฌ์ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ
8
+ # strs[i].length = k
9
+ # Time Complexity : O(n*klog(k))
10
+ # Space Complexity : O(n*k)
11
+
12
+ """
13
+
14
+ n = len(strs)
15
+ word_dict = defaultdict(list)
16
+
17
+ for word in strs:
18
+ key = hash(''.join(sorted(word)))
19
+ word_dict[key].append(word)
20
+
21
+ ret = []
22
+ for value in word_dict.values():
23
+ ret.append(value)
24
+ return ret
25
+ """
26
+
27
+ # Better Solution : Counting
28
+
29
+ # anagram ์ ํน์ฑ ์ค ์ํ๋ฒณ ์นด์ดํธ ๊ฐฏ์๊ฐ ๊ฐ๋ค๋ ๊ฒ์ ์ด์ฉ
30
+ # ์นด์ดํธ ๊ฐฏ์๋ฅผ ํ์ฉํ์ฌ key ๊ฐ์ผ๋ก ์ฒ๋ฆฌ
31
+ # Time Complexity : O(n*k)
32
+ # Space Complexity : O(n*k)
33
+ word_dict = defaultdict (list )
34
+
35
+ for word in strs :
36
+ freq = [0 ]* 26
37
+ for char in word :
38
+ freq [ord (char ) - ord ('a' )] += 1
39
+ word_dict [tuple (freq )].append (word )
40
+
41
+ return list (word_dict .values ())
42
+
43
+
44
+
Original file line number Diff line number Diff line change
1
+ """
2
+ Recursive vs Iterative
3
+
4
+ ์ฌ๊ท ๋ฐฉ์์ ๊ฒฝ์ฐ, ๋ง์ฝ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ ๊ต์ฅํ ๊ธธ๋ค๋ฉด ๊ทธ๋งํผ์ ์ฝ์ด ์ผ์ด๋๊ณ ์ด๋ ์ฑ๋ฅ์ ์ผ๋ก ๋๋ ค์ง ์ ์์.
5
+ ๋ ๊ฐ์ง ๋ชจ๋ ์๊ฐ ๋ณต์ก๋ ๋ฉด์์๋ O(m) ์ (m = len(string))
6
+
7
+ Node ํด๋์ค๋ฅผ ๋ฐ๋ก ๋์ด ์ฒ๋ฆฌํ๋ฉด ๊ฐ๋
์ฑ ๋๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค.
8
+ """
9
+
10
+ class Node :
11
+ def __init__ (self , key = None ):
12
+ self .key = key
13
+ self .children = {}
14
+ self .is_end = False
15
+
16
+ class Trie :
17
+ def __init__ (self ):
18
+ self .head = Node ()
19
+
20
+ def insert (self , word : str ) -> None :
21
+ curr = self .head
22
+
23
+ for ch in word :
24
+ if ch not in curr .children :
25
+ curr .children [ch ] = Node (ch )
26
+ curr = curr .children [ch ]
27
+ curr .is_end = True
28
+
29
+ def search (self , word : str ) -> bool :
30
+ curr = self .head
31
+
32
+ for ch in word :
33
+ if ch not in curr .children :
34
+ return False
35
+ curr = curr .children [ch ]
36
+
37
+ return curr .is_end
38
+
39
+ def startsWith (self , prefix : str ) -> bool :
40
+ curr = self .head
41
+
42
+ for ch in prefix :
43
+ if ch not in curr .children :
44
+ return False
45
+ curr = curr .children [ch ]
46
+ return True
47
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def wordBreak (self , s : str , wordDict : List [str ]) -> bool :
3
+ # DP๋ฅผ ํ์ฉํ ๋ฌธ์
4
+
5
+ """
6
+ KMP๋ Rabin Karp๋ฅผ ์ด์ฉํ์ฌ ํธ๋ ๋ฌธ์ ์ธ ์ค ์์์ผ๋ ์ ํ ์๋ ๋ฌธ์
7
+ neetcode์ ๋์์ ๋ฐ์์
8
+
9
+ ๊ณ์ ์ํ๋ฅผ ํ๋ฉด์ if dp[j]๋ฅผ ํตํด ๊ธธ์ด๋งํผ ๋์ด์ฃผ๊ณ word_set์ ์๋์ง๋ฅผ ํ์ธํจ
10
+ Time Complexity : O(n^2)
11
+ Space Complexity : O(n)
12
+ """
13
+
14
+ word_set = set (wordDict )
15
+ n = len (s )
16
+
17
+ dp = [False ] * (n + 1 )
18
+ dp [0 ] = True
19
+
20
+ for i in range (1 , n + 1 ):
21
+ for j in range (i ):
22
+ if dp [j ] and s [j :i ] in word_set :
23
+ dp [i ] = True
24
+ break
25
+
26
+ return dp [n ]
27
+
You canโt perform that action at this time.
0 commit comments