File tree Expand file tree Collapse file tree 5 files changed +124
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure
longest-increasing-subsequence Expand file tree Collapse file tree 5 files changed +124
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 시간복잡도: O(N)
2
+ # 공간복잡도: O(N)
3
+ class Solution :
4
+ def maxArea (self , height : List [int ]) -> int :
5
+ l , r = 0 , len (height ) - 1
6
+ answer = 0
7
+
8
+ while l < r :
9
+
10
+ if height [l ] < height [r ]:
11
+ answer = max (answer , (r - l ) * height [l ])
12
+ l += 1
13
+ else :
14
+ answer = max (answer , (r - l ) * height [r ])
15
+ r -= 1
16
+
17
+ return answer
Original file line number Diff line number Diff line change
1
+ class Node :
2
+ def __init__ (self , ending = False ):
3
+ self .ending = ending
4
+ self .children = {}
5
+
6
+
7
+ class WordDictionary :
8
+
9
+ def __init__ (self ):
10
+ self .head = Node (True )
11
+
12
+ # 시간복잡도: O(W)
13
+ def addWord (self , word : str ) -> None :
14
+ node = self .head
15
+
16
+ for ch in word :
17
+ if ch not in node .children :
18
+ node .children .setdefault (ch , Node ())
19
+ node = node .children [ch ]
20
+
21
+ node .ending = True
22
+
23
+ # 시간복잡도: O(W*N) W: word 길이 N: 자식 노드의 개수
24
+ def search (self , word : str ) -> bool :
25
+ def dfs (idx , node ):
26
+ if idx == len (word ):
27
+ return node .ending
28
+
29
+ if word [idx ] == '.' :
30
+ for n in node .children .values ():
31
+ if dfs (idx + 1 , n ):
32
+ return True
33
+ elif word [idx ] in node .children :
34
+ return dfs (idx + 1 , node .children [word [idx ]])
35
+ else :
36
+ return False
37
+
38
+ return dfs (0 , self .head )
Original file line number Diff line number Diff line change
1
+ # 시간복잡도: O(NlogN)
2
+ # 공간복잡도: O(N)
3
+ from bisect import bisect_left
4
+
5
+ class Solution :
6
+ def lengthOfLIS (self , nums : List [int ]) -> int :
7
+ path = []
8
+
9
+ for num in nums :
10
+ idx = bisect_left (path , num )
11
+ if len (path ) > idx :
12
+ path [idx ] = num
13
+ else :
14
+ path .append (num )
15
+
16
+ return len (path )
Original file line number Diff line number Diff line change
1
+ # 시간복잡도: O(N*M)
2
+ # 공간복잡도: O(N*M)
3
+ class Solution :
4
+ def spiralOrder (self , matrix : List [List [int ]]) -> List [int ]:
5
+ dx = [0 , 1 , 0 , - 1 ]
6
+ dy = [1 , 0 , - 1 , 0 ]
7
+ n = len (matrix [0 ])
8
+ m = len (matrix )
9
+ visited = [[False ] * n for _ in range (m )]
10
+ visited [0 ][0 ] = True
11
+ answer = []
12
+
13
+
14
+ def dfs (x , y , direction ):
15
+ answer .append (matrix [x ][y ])
16
+ nx = x + dx [direction ]
17
+ ny = y + dy [direction ]
18
+
19
+
20
+ if 0 <= nx < m and 0 <= ny < n and not visited [nx ][ny ]:
21
+ visited [nx ][ny ] = True
22
+ return dfs (nx , ny , direction )
23
+
24
+ direction = (direction + 1 ) % 4
25
+ nx = x + dx [direction ]
26
+ ny = y + dy [direction ]
27
+
28
+ if 0 <= nx < m and 0 <= ny < n and not visited [nx ][ny ]:
29
+ visited [nx ][ny ] = True
30
+ return dfs (nx , ny , direction )
31
+ else :
32
+ return answer
33
+
34
+ return dfs (0 , 0 , 0 )
Original file line number Diff line number Diff line change
1
+ # 시간복잡도: O(N)
2
+ # 공간복잡도: O(1)
3
+ class Solution :
4
+ def isValid (self , s : str ) -> bool :
5
+ if len (s ) % 2 != 0 :
6
+ return False
7
+
8
+ stack = []
9
+ matching_brackets = {'(' : ')' , '{' : '}' , '[' : ']' }
10
+
11
+ for char in s :
12
+ if char in matching_brackets :
13
+ stack .append (char )
14
+ elif stack and matching_brackets [stack [- 1 ]] == char :
15
+ stack .pop ()
16
+ else :
17
+ return False
18
+
19
+ return not stack
You can’t perform that action at this time.
0 commit comments