Skip to content

Commit 1aa5647

Browse files
authored
Merge pull request #2204 from Seoya0512/main
[Seoya0512] WEEK 06 solutions
2 parents 47786cc + e2b90ee commit 1aa5647

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+

β€Žspiral-matrix/Seoya0512.pyβ€Ž

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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

0 commit comments

Comments
Β (0)