File tree Expand file tree Collapse file tree 5 files changed +109
-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 +109
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ 시간 복잡도: O(N)
3
+ 공간 복잡도: O(1)
4
+ """
5
+ class Solution :
6
+ def maxArea (self , height : List [int ]) -> int :
7
+ result = 0
8
+ start , end = 0 , len (height ) - 1
9
+
10
+ while start < end :
11
+ area = (end - start ) * min (height [start ], height [end ])
12
+ result = max (result , area )
13
+ if height [start ] < height [end ]:
14
+ start += 1
15
+ else :
16
+ end -= 1
17
+ return result
Original file line number Diff line number Diff line change
1
+ class WordDictionary :
2
+
3
+ def __init__ (self ):
4
+ self .root = {"$" : True }
5
+
6
+
7
+ def addWord (self , word : str ) -> None :
8
+ node = self .root
9
+ for ch in word :
10
+ if ch not in node :
11
+ node [ch ] = {"$" : False }
12
+ node = node [ch ]
13
+ node ["$" ] = True
14
+
15
+ def search (self , word : str ) -> bool :
16
+ def dfs (node , idx ):
17
+ if idx == len (word ):
18
+ return node ["$" ]
19
+ ch = word [idx ]
20
+ if ch in node :
21
+ return dfs (node [ch ], idx + 1 )
22
+ elif ch == "." :
23
+ return any (dfs (node [k ], idx + 1 ) for k in node if k != "$" )
24
+ else :
25
+ return False
26
+ return dfs (self .root , 0 )
Original file line number Diff line number Diff line change
1
+ """
2
+ 시간 복잡도: O(Nlog(N))
3
+ 공간 복잡도: O(N)
4
+ """
5
+ from bisect import bisect_left
6
+
7
+ class Solution :
8
+ def lengthOfLIS (self , nums : List [int ]) -> int :
9
+ sub = []
10
+ for num in nums :
11
+ index = bisect_left (sub , num )
12
+ if index == len (sub ):
13
+ sub .append (num )
14
+ else :
15
+ sub [index ] = num
16
+ return len (sub )
Original file line number Diff line number Diff line change
1
+ """
2
+ 시간 복잡도: O(n * m)
3
+ 공간 복잡도: O(n * m)
4
+ """
5
+ class Solution :
6
+ def spiralOrder (self , matrix : List [List [int ]]) -> List [int ]:
7
+ n , m = len (matrix ), len (matrix [0 ])
8
+ dx = [1 , 0 , - 1 , 0 ]
9
+ dy = [0 , 1 , 0 , - 1 ]
10
+ state = 0
11
+ vis = [[False ] * m for _ in range (n )]
12
+
13
+ result = [matrix [0 ][0 ]]
14
+ vis [0 ][0 ] = True
15
+ x , y = 0 , 0
16
+
17
+ while len (result ) < n * m :
18
+ nx = x + dx [state % 4 ]
19
+ ny = y + dy [state % 4 ]
20
+
21
+ if not (0 <= nx < m ) or not (0 <= ny < n ) or vis [ny ][nx ]:
22
+ state += 1
23
+ continue
24
+
25
+ vis [ny ][nx ] = True
26
+ result .append (matrix [ny ][nx ])
27
+ x , y = nx , ny
28
+
29
+ return result
Original file line number Diff line number Diff line change
1
+ """
2
+ 시간 복잡도: O(N)
3
+ 공간 복잡도: O(N)
4
+ """
5
+ class Solution :
6
+ def isValid (self , s : str ) -> bool :
7
+ pair = {'(' : ')' , '{' : '}' , '[' : ']' }
8
+ stack = []
9
+ for c in s :
10
+ if c in ('(' , '{' , '[' ):
11
+ stack .append (c )
12
+ else :
13
+ if stack :
14
+ cur = stack .pop ()
15
+ if pair [cur ] == c :
16
+ continue
17
+ else :
18
+ return False
19
+ else :
20
+ return False
21
+ return not stack
You can’t perform that action at this time.
0 commit comments