Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions maximum-product-subarray/sangbeenmoon.py

Copy link
Copy Markdown
Contributor

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
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
            
                
  • 패턴: Dynamic Programming, Two Pointers
  • 설명: 연속 구간의 곱의 최댓값을 구하기 위해 현재 값과 함께 최대/최소 값의 조합을 갱신하는 방식으로, 양수/음수 곱의 부호 변화를 고려하는 DP 스타일의 상태 관리 패턴입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(1)

피드백: 한 요소를 처리할 때 현재 최대/최소 곱을 갱신하여 전체를 순회합니다. 상수 개의 변수만 사용하므로 시간은 선형이고 공간은 상수입니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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


42 changes: 42 additions & 0 deletions pacific-atlantic-water-flow/sangbeenmoon.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

pacific-atlantic-water-flow/sangbeenmoon.py
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

                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
  • 패턴: Depth-First Search, Backtracking, Dynamic Programming, Hash Map / Hash Set
  • 설명: 높이가 더 낮은 인접한 칸으로 DFS를 재귀 탐색하며 두 바다에 도달 가능한지 확인합니다. 각 셀에서 재귀를 통해 경로를 추적하고 방문 관리로 중복 탐색을 피하는 구조가 특징입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(mn(m+n))
Space O(m*n)

피드백: 매 칸마다 새 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
Loading