File tree Expand file tree Collapse file tree 4 files changed +158
-0
lines changed
best-time-to-buy-and-sell-stock
implement-trie-prefix-tree Expand file tree Collapse file tree 4 files changed +158
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ time complexity : O(n)
3+ space complexity : O(2n)
4+
5+ Algorithm : dp
6+ 주식 보유하는 경우:
7+ - 이전에 이미 보유 : hold[i - 1]
8+ - i번째 날 새로 사는 경우 : -prices[i]
9+
10+ 주식을 팔았을 떄:
11+ - 이전에 이미 팜 : sold[i - 1]
12+ - i번째 날 파는 경우 : hold[i-1] + prices[i]
13+ '''
14+
15+ class Solution :
16+ def maxProfit (self , prices : List [int ]) -> int :
17+ if not prices or len (prices ) < 2 :
18+ return 0
19+ n = len (prices )
20+ hold = [0 ] * n
21+ sold = [0 ] * n
22+
23+ hold [0 ] = - prices [0 ]
24+ sold [0 ] = 0
25+
26+ for i in range (1 , n ):
27+ hold [i ] = max (hold [i - 1 ], - prices [i ])
28+ sold [i ] = max (sold [i - 1 ], hold [i - 1 ] + prices [i ])
29+ return sold [n - 1 ]
Original file line number Diff line number Diff line change 1+ '''
2+ m : 문자열 길이
3+ n : 반복
4+ time complexity : O(m * n * log m)
5+ space complexity : O(m * n)
6+ algorithm : sort, hash,
7+ 각 문자열에 대해서 정렬을 하고, 정렬을 했을 때 동일한 문자에 대해서 hash table에 정렬 전 문자열을 추가
8+ 그리고 마지막에 list 형태로 hash table의 value에 대해서 돌려 준다
9+ nat -> ant
10+ tan -> ant
11+ '''
12+ class Solution :
13+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
14+ strResult = {}
15+ for str in strs :
16+ sortedStr = '' .join (sorted (str ))
17+ if sortedStr not in strResult :
18+ strResult [sortedStr ] = []
19+ strResult [sortedStr ].append (str )
20+ return list (strResult .values ())
Original file line number Diff line number Diff line change 1+ '''
2+ insert
3+ time complexity : O(m)
4+ space complexity : O(m)
5+
6+ search
7+ time complexity : O(m)
8+ space complexity : O(1)
9+
10+ startWith
11+ time complexity : O(m)
12+ space complexity : O(1)
13+ '''
14+
15+ class TriedNode :
16+ def __init__ (self ):
17+ self .children = {}
18+ self .isEnd = False
19+
20+ class Trie :
21+ def __init__ (self ):
22+ self .root = TriedNode ()
23+
24+ def insert (self , word : str ) -> None :
25+ node = self .root
26+ for char in word :
27+ if char not in node .children :
28+ node .children [char ] = TriedNode ()
29+ node = node .children [char ]
30+ node .isEnd = True
31+
32+
33+ def search (self , word : str ) -> bool :
34+ node = self .root
35+ for char in word :
36+ if char not in node .children :
37+ return False
38+ node = node .children [char ]
39+ return node .isEnd
40+
41+ def startsWith (self , prefix : str ) -> bool :
42+ node = self .root
43+ for char in prefix :
44+ if char not in node .children :
45+ return False
46+ node = node .children [char ]
47+ return True
48+
49+ # Your Trie object will be instantiated and called as such:
50+ # obj = Trie()
51+ # obj.insert(word)
52+ # param_2 = obj.search(word)
53+ # param_3 = obj.startsWith(prefix)
Original file line number Diff line number Diff line change 1+ '''
2+ TriedNode
3+ a : alphabet size
4+ time complexity : O(m)
5+ space complexity : O(m * a)
6+
7+ dp
8+ time complexity : O(n^2)
9+ space complexity : O(n)
10+ '''
11+ class TriedNode :
12+ def __init__ (self ):
13+ self .children = {}
14+ self .isEnd = False
15+ class Trie :
16+ def __init__ (self ):
17+ self .root = TriedNode ()
18+
19+ def insert (self , word ):
20+ node = self .root
21+ for char in word :
22+ if char not in node .children :
23+ node .children [char ] = TriedNode ()
24+ node = node .children [char ]
25+ node .isEnd = True
26+
27+ def search (self , s , start_idx ):
28+ node = self .root
29+ endPosition = []
30+
31+ for i in range (start_idx , len (s )):
32+ char = s [i ]
33+ if char not in node .children :
34+ break
35+ node = node .children [char ]
36+ if node .isEnd :
37+ endPosition .append (i + 1 )
38+ return endPosition
39+
40+ class Solution :
41+ def wordBreak (self , s : str , wordDict : List [str ]) -> bool :
42+ trie = Trie ()
43+ for word in wordDict :
44+ trie .insert (word )
45+ n = len (s )
46+ dp = [False ] * (n + 1 )
47+ dp [0 ] = True
48+
49+ for i in range (n ):
50+ if not dp [i ]:
51+ continue
52+ endPositions = trie .search (s , i )
53+
54+ for endPos in endPositions :
55+ dp [endPos ] = True
56+ return dp [n ]
You can’t perform that action at this time.
0 commit comments