-
-
Notifications
You must be signed in to change notification settings - Fork 361
[sangbeenmoon] WEEK 09 Solutions #2829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution: | ||
| def maxProduct(self, nums: List[int]) -> int: | ||
| min_val = nums[0] | ||
| max_val = nums[0] | ||
| answer = max_val | ||
|
|
||
| for i in range(1, len(nums)): | ||
| target = nums[i] | ||
| prev_max_val = max_val | ||
|
|
||
| max_val = max(max_val * target, min_val * target, target) | ||
| min_val = min(prev_max_val * target, min_val * target, target) | ||
| answer = max(answer, max_val) | ||
|
|
||
| return answer | ||
|
|
||
|
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석pacific-atlantic-water-flow/sangbeenmoon.pyclass Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
m,n = len(heights), len(heights[0])
pacific = [[False] * n for _ in range(m)]
atlantic = [[False] * n for _ in range(m)]
visited = [[False] * n for _ in range(m)]
dx = [0,0,-1,1]
dy = [-1,1,0,0]
def go(root_x:int, root_y:int, xx:int, yy:int):
for d in range(4):
nx = xx + dx[d]
ny = yy + dy[d]
if ny == -1 or nx == -1:
pacific[root_y][root_x] = True
if ny == m or nx == n:
atlantic[root_y][root_x] = True
if 0 <= ny and ny < m and 0 <= nx and nx < n:
if heights[ny][nx] <= heights[yy][xx] and not visited[ny][nx]:
visited[ny][nx] = True
go(root_x, root_y, nx, ny)
for y in range(m):
for x in range(n):
visited = [[False] * n for _ in range(m)]
go(x,y,x,y)
answer = []
for y in range(m):
for x in range(n):
if pacific[y][x] and atlantic[y][x]:
answer.append([y,x])
return answer
📊 시간/공간 복잡도 분석
피드백: 매 칸마다 새 DFS를 시작하므로 중복 방문이 많아 최악의 경우 비효율적입니다. 각 해양에 대해 역방향으로 도달 가능한 칸을 먼저 확장하는 방식이 일반적으로 더 효율적입니다. 개선 제안: 고려해볼 만한 대안: 각 해양에서 시작하는 BFS/DFS를 사용해 도달 가능한 칸을 표시한 뒤 교집합을 구하는 방식으로 시간복잡도를 O(m*n)으로 개선.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| class Solution: | ||
| def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: | ||
|
|
||
| m,n = len(heights), len(heights[0]) | ||
| pacific = [[False] * n for _ in range(m)] | ||
| atlantic = [[False] * n for _ in range(m)] | ||
|
|
||
| visited = [[False] * n for _ in range(m)] | ||
|
|
||
| dx = [0,0,-1,1] | ||
| dy = [-1,1,0,0] | ||
|
|
||
|
|
||
| def go(root_x:int, root_y:int, xx:int, yy:int): | ||
|
|
||
| for d in range(4): | ||
| nx = xx + dx[d] | ||
| ny = yy + dy[d] | ||
|
|
||
| if ny == -1 or nx == -1: | ||
| pacific[root_y][root_x] = True | ||
| if ny == m or nx == n: | ||
| atlantic[root_y][root_x] = True | ||
|
Comment on lines
+20
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ny == 0, ny == m -1 일 때 처리되는 로직을 예상했는데 -1, m 이 되었을때 처리를 하셨네요. 인덱스를 벗어난 경우로 처리해주신 이유가 있을까요? |
||
|
|
||
| if 0 <= ny and ny < m and 0 <= nx and nx < n: | ||
| if heights[ny][nx] <= heights[yy][xx] and not visited[ny][nx]: | ||
| visited[ny][nx] = True | ||
| go(root_x, root_y, nx, ny) | ||
|
|
||
| for y in range(m): | ||
| for x in range(n): | ||
| visited = [[False] * n for _ in range(m)] | ||
| go(x,y,x,y) | ||
|
|
||
| answer = [] | ||
|
|
||
| for y in range(m): | ||
| for x in range(n): | ||
| if pacific[y][x] and atlantic[y][x]: | ||
| answer.append([y,x]) | ||
|
|
||
| return answer | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
maximum-product-subarray/sangbeenmoon.py
📊 시간/공간 복잡도 분석
피드백: 한 요소를 처리할 때 현재 최대/최소 곱을 갱신하여 전체를 순회합니다. 상수 개의 변수만 사용하므로 시간은 선형이고 공간은 상수입니다.
개선 제안: 현재 구현이 적절해 보입니다.