File tree Expand file tree Collapse file tree 5 files changed +152
-0
lines changed
best-time-to-buy-and-sell-stock
implement-trie-prefix-tree Expand file tree Collapse file tree 5 files changed +152
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+
3+ # 해시맵
4+ # 시간복잡도: O(N^2)
5+ # 공간복잡도: O(N)
6+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
7+ n = len (nums )
8+ nums .sort ()
9+
10+ check = {}
11+ for idx , num in enumerate (nums ):
12+ check [num ] = idx
13+
14+ answer = set ()
15+ for i in range (n - 2 ):
16+ if nums [i ] > 0 :
17+ break
18+ if i > 0 and nums [i ] == nums [i - 1 ]:
19+ continue
20+ for j in range (i + 1 , n ):
21+ target = - (nums [i ] + nums [j ])
22+ if not check .get (target ):
23+ continue
24+ if j >= check [target ]:
25+ continue
26+
27+ answer .add ((nums [i ], nums [j ], target ))
28+
29+ return list (answer )
30+
31+ # 투포인터
32+ # 시간복잡도: O(N^2)
33+ # 공간복잡도: O(N)
34+ def threeSum2 (self , nums : List [int ]) -> List [List [int ]]:
35+ n = len (nums )
36+ nums .sort ()
37+
38+ answer = set ()
39+ for i in range (n - 2 ):
40+ if nums [i ] > 0 :
41+ break
42+ if i > 0 and nums [i ] == nums [i - 1 ]:
43+ continue
44+ l , r = i + 1 , n - 1
45+ while l < r :
46+ if nums [l ] + nums [r ] == - nums [i ]:
47+ answer .add ((nums [i ], nums [l ], nums [r ]))
48+ l += 1
49+ r -= 1
50+ while nums [l ] == nums [l - 1 ] and l < r :
51+ l += 1
52+
53+ while nums [r ] == nums [r + 1 ] and l < r :
54+ r -= 1
55+
56+ if nums [l ] + nums [r ] < - nums [i ]:
57+ l += 1
58+
59+ if nums [l ] + nums [r ] > - nums [i ]:
60+ r -= 1
61+
62+ return list (answer )
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+
6+ answer = 0
7+ cur = float (inf )
8+ for price in prices :
9+ if cur < price :
10+ answer = max (answer , price - cur )
11+
12+ if price < cur :
13+ cur = price
14+
15+ return answer
Original file line number Diff line number Diff line change 1+ # 시간복잡도: O(N*AlogA) A: 0<= strs[i]의 길이 <= 100
2+ # 공간복잡도: O(N)
3+ class Solution :
4+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
5+ groups = {}
6+ for anagram in strs :
7+ key = str (sorted (anagram ))
8+ groups .setdefault (key , []).append (anagram )
9+
10+ return list (groups .values ())
Original file line number Diff line number Diff line change 1+ class Node :
2+
3+ def __init__ (self , ending = False ):
4+ self .children = {}
5+ self .ending = ending
6+
7+ # 공간복잡도: O(w*l) w: 단어 수 l: 단어의 평균 길이
8+ class Trie :
9+
10+ def __init__ (self ):
11+ self .head = Node (ending = True )
12+
13+ # 시간복잡도: O(N)
14+ def insert (self , word : str ) -> None :
15+ node = self .head
16+ for ch in word :
17+ if ch not in node .children :
18+ node .children .setdefault (ch , Node ())
19+ node = node .children [ch ]
20+ node .ending = True
21+
22+ # 시간복잡도: O(N)
23+ def search (self , word : str ) -> bool :
24+ node = self .head
25+ for ch in word :
26+ if ch not in node .children :
27+ return False
28+ node = node .children [ch ]
29+
30+ return node .ending
31+
32+ # 시간복잡도: O(N)
33+ def startsWith (self , prefix : str ) -> bool :
34+ node = self .head
35+ for ch in prefix :
36+ if ch not in node .children :
37+ return False
38+ node = node .children [ch ]
39+
40+ return True
Original file line number Diff line number Diff line change 1+ # 시간복잡도: O(S*W)
2+ # S: s의 길이 300 W: worDict 각 단어의 총 길이 20*1000
3+ # 300 * 20*1000 = 6*1e6 (600만)
4+ # 공간복잡도: O(S)
5+ class Solution :
6+ def wordBreak (self , s : str , wordDict : List [str ]) -> bool :
7+ memo = {}
8+ def dfs (idx ):
9+ if idx in memo :
10+ return memo [idx ]
11+
12+ if idx == len (s ):
13+ return True
14+
15+ for word in wordDict :
16+ l = len (word )
17+ if s [idx :idx + l ] == word :
18+ if dfs (idx + l ):
19+ memo [idx ] = True
20+ return True
21+
22+ memo [idx ] = False
23+ return False
24+
25+ return dfs (0 )
You can’t perform that action at this time.
0 commit comments