-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
"""TC: O(n), SC: O(1) | ||
μμ΄λμ΄: | ||
λμ΄ λ¦¬μ€νΈ lμ΄ μ£Όμ΄μ‘λ€κ³ νμ. κ·Έλ¦¬κ³ μ΄ μμ μλ μ΅λ μμ‘° λ©΄μ μ f(l)μ΄λΌκ³ νμ. | ||
β‘ β‘ β‘ | ||
β‘ β‘ β‘ β‘ | ||
β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ | ||
β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ | ||
β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ | ||
2,5,3,3,1,3,1,2,2,5,3,5,3,4 | ||
^ | ||
l | ||
κ·Έλ¬λ©΄ λ€μμ΄ νμ μ±λ¦½νλ€. | ||
- lμ μ λμ κ° μ€ μμ κ° xκ° μμͺ½μ μμλ€κ³ ν΄λ³΄μ. xλ₯Ό λΊ λ¦¬μ€νΈ l`μ λ§λ λ€. | ||
- μ΄λ μμ κ°μ΄ λ·μͺ½μ μμμ΄λ μΌλ°μ±μ μμ§ μλλ€. | ||
ββ‘ β‘ β‘ | ||
ββ‘ β‘ β‘ β‘ | ||
ββ‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ | ||
β‘ββ‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ | ||
β‘ββ‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ | ||
2β5,3,3,1,3,1,2,2,5,3,5,3,4 | ||
^ ^ | ||
x l` | ||
- f(l)μ κ·Έλ λ€λ©΄ | ||
- f(l`)μ΄κ±°λ(μ¦, xλ₯Ό μ°μ§ μμκ±°λ) | ||
- xλ₯Ό μ¨μ λ§λ μμ‘° λ©΄μ μ€μ μλ€. | ||
- κ·Έλ°λ° xλ lμ μ λ κ° μ€μ μμ κ°μ΄λ―λ‘, μλμ κ°μ λΆμμ ν μ μλ€. | ||
1) xλ₯Ό μ¨μ λ§λλ μμ‘°μ λμ΄λ μ무리 λμλ xλ€. xλ³΄λ€ μμμ§ μλ μμ΄λ, xλ³΄λ€ μ»€μ§ μλ μλ€. | ||
2) xλ₯Ό μ¨μ λ§λλ μμ‘°μ νμ lμ λ€λ₯Έμͺ½ λμ μλ λμ΄λ₯Ό μ ννμλ μ΅λκ° λλ€. | ||
- κ·Έλ¬λ―λ‘, xλ₯Ό μ¨μ λ§λ€ μ μλ μμ‘°μ μ΅λ ν¬κΈ°λ lμ λ€λ₯Έμͺ½ λμ μλ λμ΄λ₯Ό μ νν κ²½μ° λμ¨λ€. | ||
μμ λ΄μ©μ μλμ μ€λͺ μ ν΅ν΄ μκ°μ μΌλ‘ νμΈν μ μλ€. | ||
- μ λμ μ νν κ²½μ° xλ‘ λ§λ€ μ μλ μ΅λ λ©΄μ μ΄λ€. | ||
ββ‘ β‘ β‘ | ||
ββ‘ β‘ β‘ β‘ | ||
ββ‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ | ||
β ββ β β β β β β β β β β β β | ||
β ββ β β β β β β β β β β β β | ||
2β5,3,3,1,3,1,2,2,5,3,5,3,4 | ||
^ ^ | ||
- xλ κ·Έλλ‘ λ μ± λ€λ₯Έμͺ½ λμ μμͺ½μΌλ‘ λ μ΄λνλ©΄ μμ‘° λμ΄λ λμΌνλ° νμ λ μμμ§λ€. | ||
ββ‘ β‘ β‘ | ||
ββ‘ β‘ β‘ β‘ | ||
ββ‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ | ||
β ββ β β β β β β β β β β β‘ β‘ | ||
β ββ β β β β β β β β β β β‘ β‘ | ||
2β5,3,3,1,3,1,2,2,5,3,5,3,4 | ||
^ ^ | ||
- μ¬μ§μ΄ xλ³΄λ€ μμ λμ΄ κ°μ μ νν κ²½μ° μμ‘° λμ΄λ μμμ§κ³ νλ μμμ§λ μΌμ΄ μΌμ΄λλ€. | ||
ββ‘ β‘ β‘ | ||
ββ‘ β‘ β‘ β‘ | ||
ββ‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ | ||
β‘ββ‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ β‘ | ||
β ββ β β β β β β‘ β‘ β‘ β‘ β‘ β‘ β‘ | ||
2β5,3,3,1,3,1,2,2,5,3,5,3,4 | ||
μ¦, μμ λ΄μ©μ μ’ ν©νλ©΄ λ€μμ νμΈν μ μλ€. | ||
- f(l) = max( (lμ μ λ λμ΄λ₯Ό μ νν΄μ λ§λ μμ‘° λμ΄), f(l`) ) | ||
- κ·Έλ°λ° f(l`)λ f(l)μ ꡬν κ²κ³Ό κ°μ λ°©μμΌλ‘ ꡬν μ μλ€. μ¦, μλ‘ λ§λ€μ΄μ§ l`μ μ λ λμ΄ μ€ | ||
짧μ μͺ½μ λΊ λ¦¬μ€νΈ l``μ λ§λ€μ΄μ μμ κ³Όμ μ λ°λ³΅ν μ μλ€. | ||
- μ¦, f(l)μ μλμ κ³Όμ μ λ°λ³΅νμ¬ κ΅¬ν μ μλ€. | ||
- lμ μ λ λμ΄λ₯Ό μ¨μ μμ‘° λμ΄λ₯Ό ꡬνκ³ , κΈ°μ‘΄ μ΅λ λμ΄μ λΉκ΅νμ¬ λ ν° κ°μ μ΅λ λμ΄μ λμ νλ€. | ||
- lμμ 짧μ μͺ½ λμ΄λ₯Ό λΊλ€. | ||
- μ κ³Όμ μ lμ μμ΄ν μ΄ νλλ§ λ¨μ λκΉμ§ λ°λ³΅. | ||
SC: | ||
- ν¬ ν¬μΈν°λ₯Ό μ¨μ lμ μμ, λ μΈλ±μ€λ₯Ό κ΄λ¦¬νλ©΄ O(1). | ||
- μμ‘° μ΅λ λμ΄κ° κ΄λ¦¬, O(1). | ||
- μ’ ν©νλ©΄ O(1). | ||
TC: | ||
- 리μ€νΈμ μ λ λμ΄λ₯Ό ν΅ν΄ λ©΄μ ꡬνκΈ°, O(1). | ||
- ν¬μΈν° μ΄λ O(1). | ||
- ν¬μΈν° μ΄λμ λ ν¬μΈν° μ¬μ΄μ κ±°λ¦¬κ° 1μ© νμ κ°μνλ―λ‘ μ κ³Όμ μ μ΅λ n-2λ² λ°λ³΅. | ||
- μ’ ν©νλ©΄ O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
max_area = -1 | ||
s, e = 0, len(height) - 1 | ||
while s < e: | ||
max_area = max(max_area, (e - s) * min(height[s], height[e])) | ||
if height[s] > height[e]: | ||
e -= 1 | ||
else: | ||
s += 1 | ||
return max_area |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""searchμ: TC: O(n), SC: O(n) | ||
wordμ max lengthκ° w, `addWord`λ₯Ό ν΅ν΄ λ§λ€μ΄μ§ λ Έλ κ°μκ° nκ°λΌκ³ νμ. | ||
μμ΄λμ΄: | ||
`implement-trie-prefix-tree` λ¬Έμ μμ ꡬνν trieμμ search λΆλΆλ§ recursiveν dfs ꡬνμΌλ‘ μμ . | ||
SC: | ||
- trieμ λ Έλ κ°μ O(n). | ||
- dfsμ μ€ν κΉμ΄λ μ΅λ w. μ¦, O(w). κ·Έλ°λ° μ€ν κΉμ΄λ λ Έλ κ°μλ³΄λ€ ν΄ μ μλ€. μ¦, O(w) < O(n). | ||
- μ’ ν©νλ©΄, O(n) + O(w) < O(n) + O(n) = O(n). | ||
TC: | ||
- μ΅μ μ κ²½μ° λͺ¨λ λ Έλ μν. O(n). | ||
""" | ||
|
||
|
||
class WordDictionary: | ||
|
||
def __init__(self): | ||
self.next: dict[str, WordDictionary] = {} | ||
self.end: bool = False | ||
|
||
def addWord(self, word: str) -> None: | ||
cur = self | ||
|
||
for c in word: | ||
cur.next[c] = cur.next.get(c, WordDictionary()) | ||
cur = cur.next[c] | ||
|
||
cur.end = True | ||
|
||
def search(self, word: str) -> bool: | ||
cur = self | ||
|
||
return self._search(cur, word, 0) | ||
|
||
def _search(self, trie, word: str, ind: int) -> bool: | ||
if ind == len(word): | ||
return trie.end | ||
|
||
c = word[ind] | ||
|
||
if c == ".": | ||
return any( | ||
[self._search(node, word, ind + 1) for node in trie.next.values()] | ||
) | ||
|
||
if c not in trie.next: | ||
return False | ||
|
||
return self._search(trie.next[c], word, ind + 1) | ||
|
||
|
||
# Your WordDictionary object will be instantiated and called as such: | ||
# obj = WordDictionary() | ||
# obj.addWord(word) | ||
# param_2 = obj.search(word) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""searchμ: TC: O(m*n), SC: O(1) | ||
μμ΄λμ΄: | ||
2μ°¨μ λ°°μ΄μμ μμ§ νκ³ κ°μ§ μμ μμμ β, β, β, β λ°©ν₯μΌλ‘ νλλ€. μ΄λ, ν λ²μ νκ³ κ° μ μ | ||
μμκ³Ό λ μν μ ν κ°λ€(μ¦, boundary κ°λ€)μ μ κ΄λ¦¬ν΄μ€λ€. | ||
- top, bottom, left, rightμ μν μ ν κ°μ κ°κ° 0, m, 0, nμΌλ‘ μ΄κΈ°ν. | ||
- β λ°©ν₯μΌλ‘ νκ³ λμ topμ ν μΉΈ μλλ‘(μ¦, top++). | ||
- β λ°©ν₯μΌλ‘ νκ³ λμ rightλ₯Ό ν μΉΈ μΌμͺ½μΌλ‘(μ¦, right--). | ||
- β λ°©ν₯μΌλ‘ νκ³ λμ bottomμ ν μΉΈ μλ‘(μ¦, bottom--). | ||
- β λ°©ν₯μΌλ‘ νκ³ λμ leftλ₯Ό ν μΉΈ μ€λ₯Έμͺ½μΌλ‘(μ¦, left++). | ||
- rightκ° leftλ³΄λ€ μΌμͺ½μ μκ±°λ topμ΄ bottomλ³΄λ€ μλ μμΌλ©΄ νμ μ’ λ£. | ||
SC: | ||
- boundary κ°λ§ κ΄λ¦¬. O(1). | ||
TC: | ||
- λ°°μ΄μ λͺ¨λ μμ΄ν μ μ νν ν λ²μ© μ κ·Ό. O(m*n). | ||
- μ μ ν λ² κΈλ κ³Όμ μμ μΌμ΄λλ μΌμ΄ μ΄ O(1)μΈλ°(μ½λ μ°Έμ‘°), μ΄κ² μ무리 λ§μλ O(m*n)μ λμ§λ | ||
λͺ»νλ€. ν μ μ μ΅μ νλμ κ°μ λ€μ΄μκΈ° λλ¬Έ. | ||
- μ’ ν©νλ©΄ O(m*n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: | ||
def read_line(start: tuple[int, int], end: tuple[int, int]) -> list[int]: | ||
if start[0] == end[0]: | ||
# column λ°©ν₯ | ||
s, e = sorted([start[1], end[1]]) # TC: O(1) | ||
line = range(s, e + 1) | ||
if start[1] > end[1]: # TC: O(1) | ||
line = reversed(line) | ||
return [matrix[i][start[0]] for i in line] | ||
else: | ||
# row λ°©ν₯ | ||
s, e = sorted([start[0], end[0]]) # TC: O(1) | ||
line = range(s, e + 1) | ||
if start[0] > end[0]: # TC: O(1) | ||
line = reversed(line) | ||
return [matrix[start[1]][i] for i in line] | ||
|
||
top, bottom, left, right = 0, len(matrix) - 1, 0, len(matrix[0]) - 1 | ||
sol = [] | ||
while True: | ||
# μ | ||
if right < left: | ||
break | ||
line = read_line((left, top), (right, top)) | ||
sol += line | ||
top += 1 # TC: O(1) | ||
|
||
# μ€λ₯Έμͺ½ | ||
if top > bottom: | ||
break | ||
line = read_line((right, top), (right, bottom)) | ||
sol += line | ||
right -= 1 # TC: O(1) | ||
|
||
# μλ | ||
if right < left: | ||
break | ||
line = read_line((right, bottom), (left, bottom)) | ||
sol += line | ||
bottom -= 1 # TC: O(1) | ||
|
||
# μΌμͺ½ | ||
if top > bottom: | ||
break | ||
line = read_line((left, bottom), (left, top)) | ||
sol += line | ||
left += 1 # TC: O(1) | ||
|
||
return sol |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""TC: O(n), SC: O(n) | ||
λ¬Έμμ΄ κΈΈμ΄ n | ||
μμ΄λμ΄: | ||
- λ¬Έμμ΄μ μμμλΆν° νμΌλ©΄μ | ||
- κ΄νΈ μμμ λ°κ²¬νλ©΄ μ€νμ λ£λλ€. | ||
- κ΄νΈ λμ λ°κ²¬νλ©΄ μ€νμ μ μΌ λ§μ§λ§μ μλ μμ΄ν μ popν΄μ 맀μΉμ΄ λλμ§ νμΈνλ€. | ||
- μ΄λ λ€μκ³Ό κ°μ μμΈ μν©λ€μ μ‘°μ¬νλ€. | ||
- μ€νμ μμ΄ν μ΄ νλλ μλλ° popμ νλ €λ μν© λ°©μ§. | ||
- e.g.) "))))" | ||
- λͺ¨λ λ¬Έμμ΄μ νκ³ μ§λκ°λλ° μ€νμ μμ΄ν μ΄ λ¨μμλ κ²½μ°λ μ€ν¨λ€. | ||
- e.g.) "((((" | ||
SC: | ||
- μμ κ΄νΈ 리μ€νΈ O(1). | ||
- λ κ΄νΈμ 맀μΉλλ μμ κ΄νΈ dict O(1). | ||
- μμ κ΄νΈλ§ μλ κ²½μ° μ€νμ μμ΄λ μμ΄ν μ μ΄ nκ°λ€. O(n). | ||
- μ’ ν©νλ©΄ O(n). | ||
TC: | ||
- λ¬Έμ μ΄ nκ°μ λν΄ μλλ₯Ό λ°λ³΅νλ€. | ||
- μμ κ΄νΈμΈμ§ 체ν¬. O(1). | ||
- μμ κ΄νΈμΈ κ²½μ° μ€νμ μκΈ° O(1). | ||
- λ κ΄νΈμΈ κ²½μ° μ€νμμ popν λ O(1), κ΄νΈ λ§€μΉ μ²΄ν¬μ O(1). | ||
- μ’ ν©νλ©΄ O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
stack = [] | ||
openings = ["(", "{", "["] | ||
ending_match = { | ||
")": "(", | ||
"}": "{", | ||
"]": "[", | ||
} | ||
for c in s: | ||
if c in openings: | ||
stack.append(c) | ||
else: | ||
if not stack or ending_match[c] != stack.pop(): | ||
return False | ||
return False if stack else True |