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)