Skip to content

Commit 5862e65

Browse files
authored
Merge pull request #2531 from kangdaia/main
[kangdaia] WEEK 6 solutions
2 parents b422044 + b41a689 commit 5862e65

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def maxArea(self, height: list[int]) -> int:
3+
# ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ ์žˆ์œผ๋ฉด์„œ ๋†’์ด๊ฐ€ ๋น„์Šทํ•œ ๋ง‰๋Œ€๊ธฐ?
4+
max_area = 0
5+
i, j = 0, len(height) - 1
6+
while i < j:
7+
left, right = height[i], height[j]
8+
area = min(left, right) * (j - i)
9+
if area > max_area:
10+
max_area = area
11+
if left < right:
12+
i += 1
13+
else:
14+
j -= 1
15+
return max_area

โ€Žspiral-matrix/kangdaia.pyโ€Ž

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution:
2+
def spiralOrder(self, matrix: list[list[int]]) -> list[int]:
3+
"""
4+
์ฃผ์–ด์ง„ 2D ํ–‰๋ ฌ์„ ๋‚˜์„ ํ˜•์œผ๋กœ ์ˆœํšŒํ•˜์—ฌ ์š”์†Œ๋“ค์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
5+
6+
๋ฐฉ๋ฒ•:
7+
1. transpose ํ–‰๋ ฌ์„ ๋งŒ๋“ค์–ด ์—ด์„ ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
8+
2. while ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚˜์„ ํ˜•์œผ๋กœ ์ˆœํšŒ
9+
3. ๊ฐ ๋‹จ๊ณ„์—์„œ top row, right column, bottom row, left column์„ ์ˆœ์„œ๋Œ€๋กœ ์ถ”๊ฐ€
10+
4. ๊ฐ ๋‹จ๊ณ„๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค row์™€ col์„ ์ฆ๊ฐ€์‹œ์ผœ ๋‹ค์Œ ๋ ˆ์ด์–ด๋กœ ์ด๋™
11+
12+
์‹œ๊ฐ„๋ณต์žก๋„ O(m*n), ๊ณต๊ฐ„๋ณต์žก๋„ O(m*n)
13+
14+
Args:
15+
matrix (list[list[int]]): 2D ํ–‰๋ ฌ
16+
17+
Returns:
18+
list[int]: ๋‚˜์„ ํ˜•์œผ๋กœ ์ˆœํšŒํ•œ ์š”์†Œ๋“ค์˜ ๋ฆฌ์ŠคํŠธ
19+
"""
20+
row, col = 0, 0
21+
m, n = len(matrix), len(matrix[0])
22+
t_matrix = list(zip(*matrix))
23+
spiral = []
24+
while row < m and col < n:
25+
top, left = row, col
26+
bottom, right = m - row - 1, n - col - 1
27+
if top > bottom or left > right:
28+
break
29+
# first row: left -> right
30+
first_row = matrix[top][left : right + 1]
31+
spiral += first_row
32+
# last col: top+1 -> bottom
33+
last_col = list(t_matrix[right][top + 1 : bottom + 1])
34+
spiral += last_col
35+
# last row: right-1 -> left (์—ญ์ˆœ)
36+
if top < bottom:
37+
last_row = matrix[bottom][left:right][::-1]
38+
spiral += last_row
39+
# first col: bottom-1 -> top+1 (์—ญ์ˆœ)
40+
if left < right:
41+
first_col = list(t_matrix[left][top + 1 : bottom][::-1])
42+
spiral += first_col
43+
# add
44+
row += 1
45+
col += 1
46+
return spiral
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
"""s์— ์žˆ๋Š” bracket๋“ค์˜ ์Œ์ด ๋งž๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
4+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
5+
6+
Args:
7+
s (str): bracket {}, [], ()๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด
8+
9+
Returns:
10+
bool: bracket๋“ค์˜ ์Œ์ด ๋งž๋Š”์ง€ ์—ฌ๋ถ€
11+
"""
12+
stack = []
13+
pair = {"{": "}", "[": "]", "(": ")"}
14+
for ch in s:
15+
if ch in pair:
16+
stack.append(ch)
17+
elif len(stack) == 0:
18+
return False
19+
else:
20+
last = stack.pop()
21+
if pair[last] != ch:
22+
return False
23+
return True if len(stack) == 0 else False

0 commit comments

Comments
ย (0)