File tree Expand file tree Collapse file tree 5 files changed +122
-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 +122
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ μ΄μ€ For-loopμ μ¬μ©νλ©΄ μκ°λ³΅μ‘λκ° O(n^2)μ΄ λλ―λ‘ μκ°μ΄κ³Όκ° λ°μ
3+ 리νΈμ½λμ Hintλ₯Ό μ°Έκ³ ν΄ Two Pointerλ₯Ό μ¬μ©νλ λ°©μμΌλ‘ μλνμ΅λλ€.
4+
5+ Time Complexity: O(n)
6+ - While 루νλ₯Ό μ¬μ©ν΄μ μΈλ±μ€ iμ jκ° λ§λ λκΉμ§ λ°λ³΅μΌλ‘ μ§ννκΈ°μ O(n)
7+
8+ Space Complexity: O(1)
9+ - height κ°κ³Ό, max_area κ° λͺ¨λ μμ 곡κ°μ μ¬μ©
10+ '''
11+ class Solution :
12+ def maxArea (self , height : List [int ]) -> int :
13+ i , j = 0 , len (height ) - 1
14+ max_area = 0
15+
16+ while i < j :
17+ area = min (height [i ], height [j ]) * (j - i )
18+ max_area = max (area , max_area )
19+
20+ if height [i ] < height [j ]:
21+ i += 1
22+ else :
23+ j -= 1
24+
25+ return max_area
26+
Original file line number Diff line number Diff line change 1+ '''
2+ μ΄λ² λ¬Έμ λ Trie μλ£κ΅¬μ‘°λ₯Ό νμ©ν λ¬Έμ μμ΅λλ€.
3+ μ΄λ₯Ό νμ
νμ§ λͺ»ν΄μ μ 체 νμνλ ꡬ쑰λ₯Ό λ§λ€μλ€ κ²°κ΅ λ λμμ λ°μ μ΄ν΄νλ κ²μ λͺ©νλ₯Ό λμ΅λλ€.
4+ '''
5+ class WordDictionary :
6+ def __init__ (self ):
7+ self .root = {"$" : True }
8+
9+ def addWord (self , word : str ) -> None :
10+ node = self .root
11+ for ch in word :
12+ if ch not in node :
13+ node [ch ] = {"$" : False }
14+ node = node [ch ]
15+ node ["$" ] = True
16+
17+ def search (self , word : str ) -> bool :
18+ def dfs (node , idx ):
19+ if idx == len (word ):
20+ return node ["$" ]
21+ ch = word [idx ]
22+ if ch in node :
23+ return dfs (node [ch ], idx + 1 )
24+ if ch == "." :
25+ return any (dfs (node [k ], idx + 1 ) for k in node if k != "$" )
26+ return False
27+ return dfs (self .root , 0 )
28+
Original file line number Diff line number Diff line change 1+ '''
2+ Dynamic Programming λ¬Έμ λ‘ λμ ν©μ μ΄μ©ν΄ νμ΄λ³΄λ € νμΌλ λλ΄ ν΄κ²°μ λͺ»ν΄ μ°Έκ³ νμ΅λλ€.
3+ '''
4+ class Solution :
5+ def lengthOfLIS (self , nums : List [int ]) -> int :
6+ dp = [1 ] * len (nums )
7+ for cur in range (1 , len (nums )):
8+ for prev in range (len (nums )):
9+ if nums [prev ] < nums [cur ]:
10+ dp [cur ] = max (1 + dp [prev ], dp [cur ])
11+ return max (dp )
12+
Original file line number Diff line number Diff line change 1+ '''
2+ ν΄λΉ λ¬Έμ λ₯Ό νμΈνκ³
3+ μλ¨ -> μ°μΈ‘ -> νλ¨ -> μ’μΈ‘ λ°©ν₯μΌλ‘ λμ νμΌλ‘ μ κ·Όνλ κ²μ μ°μ μμλ‘ μκ°νμ΅λλ€.
4+ popμ ν΅ν΄μ matrixμ νκ³Ό μ΄μ μ κ±°νλ©΄μ μ κ·Όνλ λ°©μμ μ¬μ©ν΄ ν΄κ²°νμ΅λλ€.
5+
6+ μ΄λ κ² λ¬Έμ λ₯Ό νμμλ popμ μ¬μ©νμ¬ νκ³Ό μ΄μ μ κ±°νκΈ° λλ¬Έμ
7+ λΉμΌ κ³μ° λΉμ©μ΄ λ°μνλ λΆλΆμ κ³ λ €νμ§ λͺ»νλ€λ μ μ΄ μμ½κ² λκ»΄μ§λ€μ.
8+
9+ Time Complexity: O(mxn)
10+ - m: νμ μλ§νΌ μ κ·Ό
11+ - n: μ΄μ μλ§νΌ μ κ·Ό
12+
13+ Space Complexity: O(mxn)
14+ - result 리μ€νΈκ° μ 체 μμλ₯Ό μ μ₯
15+ '''
16+ class Solution :
17+ def spiralOrder (self , matrix : List [List [int ]]) -> List [int ]:
18+ result = []
19+ idx = 0 # μ²μ κ°
20+ while matrix :
21+ result += matrix .pop (idx )
22+ for i in range (len (matrix )):
23+ if matrix [i ]:
24+ result .append (matrix [i ].pop ())
25+
26+ # λΉ ν μ κ±° (μ€μ!)
27+ matrix = [row for row in matrix if row ]
28+ if not matrix :
29+ break
30+
31+ result += matrix .pop ()[::- 1 ]
32+
33+ for j in range (len (matrix )- 1 , - 1 , - 1 ):
34+ if matrix [j ]:
35+ result .append (matrix [j ].pop (0 ))
36+
37+ return result
38+
Original file line number Diff line number Diff line change 1+ '''
2+ 20. Valid Parentheses
3+ Stackμ μ¬μ©νλ λ¬Έμ μμ νμ
νμ§ λͺ»ν΄μ κ²°κ΅ μκ³ λ¬λ λμ λμμ λ°μμ΅λλ€.
4+
5+ Time Complexity: O(n)
6+ Space Complexity: O(n)
7+ '''
8+ class Solution :
9+ def isValid (self , s : str ) -> bool :
10+ parens = {"(" : ")" , "{" : "}" , "[" : "]" }
11+ stack = []
12+ for val in s :
13+ if val in parens :
14+ stack .append (val )
15+ else :
16+ if not stack or val != parens [stack .pop ()]:
17+ return False
18+ return not stack
You canβt perform that action at this time.
0 commit comments