Skip to content

Commit 4109f2e

Browse files
authored
Merge pull request DaleStudy#471 from mangodm-web/main
[mangodm-web] Week 06 Solutions
2 parents cd07110 + ce4b722 commit 4109f2e

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def maxArea(self, height: List[int]) -> int:
6+
"""
7+
- Idea: 배열의 양쪽 끝에서 시작하는 두 포인터(left, right)를 이용해 두 선 사이의 최대 영역을 구한다. 둘 중, 높이가 낮은 쪽의 포인터는 중앙 쪽으로 이동시킨다.
8+
- Time Complexity: O(n), n은 주어진 배열(height)의 길이.
9+
- Space Complexity: O(1), 추가 공간은 사용하지 않는다.
10+
"""
11+
left, right = 0, len(height) - 1
12+
result = 0
13+
14+
while left < right:
15+
current_width = right - left
16+
current_height = min(height[left], height[right])
17+
result = max(result, current_width * current_height)
18+
19+
if height[left] < height[right]:
20+
left += 1
21+
else:
22+
right -= 1
23+
24+
return result

spiral-matrix/mangodm-web.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
6+
"""
7+
- Idea: 네 개의 포인터(left, right, top, bottom)를 활용하여 행렬의 바깥쪽부터 안쪽으로 순회한다.
8+
- left, right: 행렬의 왼쪽과 오른쪽 끝. 순회하면서 점점 좁혀진다.
9+
- top, bottom: 행렬의 위쪽과 아래쪽. 순회하면서 점점 좁혀진다.
10+
- Time Complexity: O(m*n), m, n은 각각 주어진 행렬(matrix)의 행과 열의 개수. 행렬의 모든 요소를 한번씩 접근한다.
11+
- Space Complexity: O(1), 결과 리스트(result)를 제외하고 포인터를 위한 상수 크기의 변수 이외의 추가 공간은 사용하지 않는다.
12+
"""
13+
result = []
14+
left, right = 0, len(matrix[0])
15+
top, bottom = 0, len(matrix)
16+
17+
while left < right and top < bottom:
18+
for i in range(left, right):
19+
result.append(matrix[top][i])
20+
top += 1
21+
22+
for i in range(top, bottom):
23+
result.append(matrix[i][right - 1])
24+
right -= 1
25+
26+
if not (left < right and top < bottom):
27+
break
28+
29+
for i in range(right - 1, left - 1, -1):
30+
result.append(matrix[bottom - 1][i])
31+
bottom -= 1
32+
33+
for i in range(bottom - 1, top - 1, -1):
34+
result.append(matrix[i][left])
35+
left += 1
36+
37+
return result

valid-parentheses/mangodm-web.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
"""
4+
- Idea: 주어진 문자열을 순회하면서 여는 괄호는 스택에 넣고, 닫는 괄호는 스택의 최상단 요소와 매칭되는지 확인한다.
5+
- Time Complexity: O(n), n은 주어진 문자열의 길이. 모든 문자를 한번씩은 순회한다.
6+
- Space Complexity: O(n), 주어진 문자열이 모두 여는 괄호일 경우 스택에 저장된다.
7+
"""
8+
bracket_map = {"(": ")", "[": "]", "{": "}"}
9+
stack = []
10+
11+
for char in s:
12+
if char in bracket_map:
13+
stack.append(char)
14+
elif not stack or bracket_map[stack.pop()] != char:
15+
return False
16+
17+
return not stack

0 commit comments

Comments
 (0)