File tree Expand file tree Collapse file tree 5 files changed +91
-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 +91
-0
lines changed Original file line number Diff line number Diff line change 1+ # 시간 복잡도 : O(n)
2+ # 공간 복잡도 : O(1)
3+ class Solution :
4+ def maxProfit (self , prices : List [int ]) -> int :
5+ min_price = float ('inf' )
6+ max_profit = 0
7+
8+ for price in prices :
9+ min_price = min (min_price , price )
10+ max_profit = max (max_profit , price - min_price )
11+
12+ return max_profit
13+
Original file line number Diff line number Diff line change 1+ # 시간복잡도: O(n)
2+ # 공간복잡도: O(1)
3+
4+ class Solution :
5+ """
6+ @param: strs: a list of strings
7+ @return: encodes a list of strings to a single string.
8+ """
9+ def encode (self , strs ):
10+ if not strs :
11+ return ""
12+ return chr (257 ).join (strs )
13+
14+ """
15+ @param: str: A string
16+ @return: decodes a single string to a list of strings
17+ """
18+ def decode (self , str ):
19+ if not str :
20+ return []
21+ return str .split (chr (257 ))
22+
Original file line number Diff line number Diff line change 1+ # 시간복잡도 : O(n*mlogm) (n은 strs의 길이, m은 strs의 원소의 길이)
2+ # 공간복잡도 : O(n)
3+
4+ class Solution :
5+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
6+ anagrams = collections .defaultdict (list )
7+ for word in strs :
8+ anagrams ['' .join (sorted (word ))].append (word )
9+ return list (anagrams .values ())
10+
Original file line number Diff line number Diff line change 1+ # 시간복잡도: O(n) (n은 문자열의 길이)
2+ # 공간복잡도: O(n) (n은 문자열의 길이)
3+ class Trie :
4+ def __init__ (self ):
5+ self .root = {}
6+ self .end = False
7+
8+ def insert (self , word : str ) -> None :
9+ node = self .root
10+ for char in word :
11+ if char not in node :
12+ node [char ] = {}
13+ node = node [char ]
14+ node [self .end ] = True
15+
16+ def search (self , word : str ) -> bool :
17+ node = self .root
18+ for char in word :
19+ if char not in node :
20+ return False
21+ node = node [char ]
22+ return self .end in node
23+
24+ def startsWith (self , prefix : str ) -> bool :
25+ node = self .root
26+ for char in prefix :
27+ if char not in node :
28+ return False
29+ node = node [char ]
30+ return True
31+
Original file line number Diff line number Diff line change 1+ # 시간복잡도 : O(n^2)
2+ # 공간복잡도 : O(n)
3+ class Solution :
4+ def wordBreak (self , s : str , wordDict : List [str ]) -> bool :
5+ dp = [False ] * (len (s ) + 1 )
6+ dp [0 ] = True
7+
8+ for i in range (1 , len (s ) + 1 ):
9+ for j in range (i ):
10+ if dp [j ] and s [j :i ] in wordDict :
11+ dp [i ] = True
12+ break
13+
14+ return dp [- 1 ]
15+
You can’t perform that action at this time.
0 commit comments