Skip to content

Commit 42d1f47

Browse files
authored
Merge pull request DaleStudy#2513 from gcount85/main
[gcount85] WEEK 06 Solutions
2 parents 5862e65 + 177774e commit 42d1f47

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
# Approach
3+
ํˆฌํฌ์ธํ„ฐ๋กœ ์–‘ ๋์—์„œ ๋ฌผ์˜ ์–‘์„ ๊ณ„์‚ฐํ•ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค.
4+
๋” ๋‚ฎ์€ height์ธ ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ค„์—ฌ๋‚˜๊ฐ€๋Š”๋ฐ, ๋†’์ด๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์™ผ์ชฝ ํฌ์ธํ„ฐ๋งŒ ์›€์ง์ž…๋‹ˆ๋‹ค.
5+
6+
# Complexity
7+
height์˜ ๊ธธ์ด๊ฐ€ N์ผ ๋•Œ
8+
- Time complexity: O(N)
9+
- Space complexity: O(1)
10+
"""
11+
12+
13+
class Solution:
14+
def maxArea(self, height: list[int]) -> int:
15+
n = len(height)
16+
left, right = 0, n - 1
17+
best = 0
18+
while left < right:
19+
best = max(best, (right - left) * min(height[left], height[right]))
20+
if height[left] > height[right]:
21+
right -= 1
22+
else:
23+
left += 1
24+
return best
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
# Approach
3+
dp[n]์€ nums[n] ์œ„์น˜๊นŒ์ง€ lis ๊ธธ์ด์ž…๋‹ˆ๋‹ค.
4+
5+
# Complexity
6+
nums์˜ ๊ธธ์ด๋ฅผ n์ด๋ผ๊ณ  ํ•  ๋•Œ,
7+
- Time complexity: ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ O(N^2)
8+
- Space complexity: dp ๋ฐฐ์—ด O(N)
9+
"""
10+
11+
12+
class Solution:
13+
def lengthOfLIS(self, nums: list[int]) -> int:
14+
n = len(nums)
15+
dp = [1] * n
16+
answer = 1
17+
for i in range(1, n):
18+
for j in range(i):
19+
if nums[j] < nums[i]:
20+
dp[i] = max(dp[j] + 1, dp[i])
21+
answer = max(answer, dp[i])
22+
return answer

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
# Approach
3+
์ƒํ•˜์ขŒ์šฐ ๊ฒฝ๊ณ„๊ฐ’์„ ์„ค์ •ํ•˜๊ณ  matrix์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ฒฝ๊ณ„๊ฐ’์„ ์ค„์—ฌ๋‚˜๊ฐ‘๋‹ˆ๋‹ค.
4+
5+
# Complexity
6+
matrix ํฌ๊ธฐ์˜ ๊ฐ€๋กœ๋ฅผ M, ์„ธ๋กœ๋ฅผ N์ด๋ผ๊ณ  ํ•  ๋•Œ
7+
- Time complexity: O(M*N)
8+
- Space complexity: O(M*N)
9+
"""
10+
11+
12+
class Solution:
13+
def spiralOrder(self, matrix: list[list[int]]) -> list[int]:
14+
start_row, start_col = 0, 0
15+
end_row, end_col = len(matrix) - 1, len(matrix[0]) - 1
16+
output = []
17+
18+
while start_row <= end_row and start_col <= end_col:
19+
# ์ขŒ->์šฐ
20+
for col in range(start_col, end_col + 1):
21+
output.append(matrix[start_row][col])
22+
start_row += 1
23+
24+
# ์ƒ->ํ•˜
25+
for row in range(start_row, end_row + 1):
26+
output.append(matrix[row][end_col])
27+
end_col -= 1
28+
29+
# ์šฐ->์ขŒ
30+
if start_row <= end_row:
31+
for col in range(end_col, start_col - 1, -1):
32+
output.append(matrix[end_row][col])
33+
end_row -= 1
34+
35+
# ํ•˜->์ƒ
36+
if start_col <= end_col:
37+
for row in range(end_row, start_row - 1, -1):
38+
output.append(matrix[row][start_col])
39+
start_col += 1
40+
41+
return output
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
# Approach
3+
์Šคํƒ์„ ์ด์šฉํ•˜์—ฌ ๋‹ซ๋Š” ๊ด„ํ˜ธ๊ฐ€ ๋‚˜์˜ฌ ๋•Œ ์Šคํƒ top๊ณผ ๋น„๊ตํ•˜์—ฌ ์ง์ด ๋งž์œผ๋ฉด popํ•ฉ๋‹ˆ๋‹ค.
4+
์ง์ด ๋งž์ง€ ์•Š์œผ๋ฉด invalid, ์ตœ์ข…์ ์œผ๋กœ ์Šคํƒ์ด ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด invalid ํ•ฉ๋‹ˆ๋‹ค.
5+
6+
# Complexity
7+
s์˜ ๊ธธ์ด๋ฅผ N์ด๋ผ๊ณ  ํ•  ๋•Œ
8+
- Time complexity: O(N)
9+
- Space complexity: O(N)
10+
"""
11+
12+
13+
class Solution:
14+
def isValid(self, s: str) -> bool:
15+
stack = []
16+
brackets = {"(": ")", "{": "}", "[": "]"}
17+
for b in s:
18+
if b in brackets:
19+
stack.append(b)
20+
continue
21+
if not stack or brackets[stack.pop()] != b:
22+
return False
23+
return not stack

0 commit comments

Comments
ย (0)