File tree Expand file tree Collapse file tree 6 files changed +119
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings
implement-trie-prefix-tree Expand file tree Collapse file tree 6 files changed +119
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ """
3+ 1. ๋ธ๋ฃจํธํฌ์ค
4+ 2์ค for๋ฌธ์ด๋ผ์ O(n^2)์.
5+ prices์ ๊ธธ์ด๊ฐ 10^5์ด๋ฏ๋ก 10^10์ด ๋๋ฉด์ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์
6+
7+ 2. ์ด๋ถํ์์ผ๋ก ๊ฐ๋ฅํ ๊น ํ์ง๋ง DP๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ๋ฌธ์ ๊ฐ์
8+ ์๊ฐ๋ณต์ก๋๋ O(n)์ด ๋์ด
9+ """
10+ """
11+ def maxProfit(self, prices: List[int]) -> int:
12+ max_profit = 0
13+ for i in range(len(prices)-1):
14+ for j in range(i, len(prices)):
15+ profit = prices[j] - prices[i]
16+ max_profit = max(max_profit, profit)
17+ return max_profit
18+ """
19+ def maxProfit (self , prices : List [int ]) -> int :
20+ max_profit = 0
21+ min_price = prices [0 ]
22+ for price in prices :
23+ max_profit = max (max_profit , price - min_price )
24+ min_price = min (price , min_price )
25+ 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+ def encode (self , strs ):
7+ return "secretKey!@#" .join (strs )
8+
9+ """
10+ @param: str: A string
11+ @return: decodes a single string to a list of strings
12+ """
13+ def decode (self , str ):
14+ return str .split ("secretKey!@#" )
Original file line number Diff line number Diff line change 1+ from collections import defaultdict
2+
3+ class Solution :
4+ """
5+ 1. ์ ๋ ฌ๋ ๊ฐ์ด ๋์
๋๋ฆฌ์ ์์ผ๋ฉด ๋ฆฌ์คํธ ํ์์ผ๋ก ์ฝ์
6+ """
7+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
8+ dict_strs = defaultdict (list )
9+
10+ for word in strs :
11+ dict_strs [str (sorted (word ))].append (word )
12+ return list (dict_strs .values ())
Original file line number Diff line number Diff line change 1+ class Trie :
2+
3+ def __init__ (self ):
4+ self .root = {"$" : True }
5+
6+ def insert (self , word : str ) -> None :
7+ node = self .root
8+ for ch in word :
9+ if ch not in node :
10+ node [ch ] = {"$" : False }
11+ node = node [ch ]
12+ node ["$" ] = True
13+
14+ def search (self , word : str ) -> bool :
15+ node = self .root
16+ for ch in word :
17+ if ch not in node :
18+ return False
19+ node = node [ch ]
20+ return node ["$" ]
21+
22+ def startsWith (self , prefix : str ) -> bool :
23+ node = self .root
24+ for ch in prefix :
25+ if ch not in node :
26+ return False
27+ node = node [ch ]
28+ return True
29+
30+
31+ # Your Trie object will be instantiated and called as such:
32+ # obj = Trie()
33+ # obj.insert(word)
34+ # param_2 = obj.search(word)
35+ # param_3 = obj.startsWith(prefix)
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ public boolean wordBreak (String s , List <String > wordDict ) {
5+ Set <String > wordSet = new HashSet <>(wordDict );
6+ boolean [] dp = new boolean [s .length () + 1 ];
7+ dp [0 ] = true ;
8+
9+ for (int i = 1 ; i < s .length () + 1 ; i ++) {
10+ for (int j = 0 ; j < i ; j ++) {
11+ if (dp [j ] && (wordSet .contains (s .substring (j , i )))) {
12+ dp [i ] = true ;
13+ break ;
14+ }
15+ }
16+ }
17+ return dp [s .length ()];
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def wordBreak (self , s : str , wordDict : List [str ]) -> bool :
3+ word_set = set (wordDict ) # O(1) ์กฐํ๋ฅผ ์ํด set์ผ๋ก ๋ณํ
4+ n = len (s )
5+ dp = [False ] * (n + 1 )
6+ dp [0 ] = True # ๊ณต์งํฉ์ ํญ์ ๊ฐ๋ฅ
7+
8+ for i in range (1 , n + 1 ):
9+ for j in range (i ):
10+ if dp [j ] and s [j :i ] in word_set :
11+ dp [i ] = True
12+ break # i๋ฒ์งธ๊น์ง ๋๋ ์ ์์ผ๋ฉด ๋ ํ์ธํ ํ์ ์์
13+
14+ return dp [- 1 ]
You canโt perform that action at this time.
0 commit comments