Skip to content
Open
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
24 changes: 24 additions & 0 deletions container-with-most-water/gcount85.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 이 코드는 양 끝에서 시작하여 포인터를 이동시키며 최대 용량을 찾는 방식으로, 투포인터 패턴을 사용합니다. 효율적인 탐색을 위해 양쪽 포인터를 조절하는 전략이 핵심입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
# Approach
투포인터로 양 끝에서 물의 양을 계산해나갑니다.
더 낮은 height인 쪽의 포인터를 줄여나가는데, 높이가 같은 경우에는 양쪽 모두 포인터를 움직입니다.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

설명이 구현과 다른 것 같네요?

Suggested change
낮은 height인 쪽의 포인터를 줄여나가는데, 높이가 같은 경우에는 양쪽 모두 포인터를 움직입니다.
낮은 height인 쪽의 포인터를 줄여나가는데, 높이가 같은 경우에는 왼쪽 포인터만 움직입니다.


# Complexity
height의 길이가 N일 때
- Time complexity: O(N)
- Space complexity: O(1)
"""


class Solution:
def maxArea(self, height: list[int]) -> int:
n = len(height)
left, right = 0, n - 1
best = 0
while left < right:
best = max(best, (right - left) * min(height[left], height[right]))
if height[left] > height[right]:
right -= 1
else:
left += 1
return best
22 changes: 22 additions & 0 deletions longest-increasing-subsequence/gcount85.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 LIS 문제 해결을 위해 DP 배열을 사용하며, 이전 값들을 참고하여 최장 증가 부분수열을 찾는 방식입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
# Approach
dp[n]은 nums[n] 위치까지 lis 길이입니다.

# Complexity
nums의 길이를 n이라고 할 때,
- Time complexity: 이중 반복문 O(N^2)
- Space complexity: dp 배열 O(N)
"""


class Solution:
def lengthOfLIS(self, nums: list[int]) -> int:
n = len(nums)
dp = [1] * n
answer = 1
for i in range(1, n):
for j in range(i):
if nums[j] < nums[i]:
dp[i] = max(dp[j] + 1, dp[i])
answer = max(answer, dp[i])
return answer
41 changes: 41 additions & 0 deletions spiral-matrix/gcount85.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 이 코드는 행과 열의 경계값을 조절하며 순차적으로 탐색하는 방식으로, 양 끝을 가리키는 포인터를 이동시키는 두 포인터 패턴에 속합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
# Approach
상하좌우 경계값을 설정하고 matrix을 순회하며 경계값을 줄여나갑니다.

# Complexity
matrix 크기의 가로를 M, 세로를 N이라고 할 때
- Time complexity: O(M*N)
- Space complexity: O(M*N)
"""


class Solution:
def spiralOrder(self, matrix: list[list[int]]) -> list[int]:
start_row, start_col = 0, 0
end_row, end_col = len(matrix) - 1, len(matrix[0]) - 1
output = []

while start_row <= end_row and start_col <= end_col:
# 좌->우
for col in range(start_col, end_col + 1):
output.append(matrix[start_row][col])
start_row += 1

# 상->하
for row in range(start_row, end_row + 1):
output.append(matrix[row][end_col])
end_col -= 1

# 우->좌
if start_row <= end_row:
for col in range(end_col, start_col - 1, -1):
output.append(matrix[end_row][col])
end_row -= 1

# 하->상
if start_col <= end_col:
for row in range(end_row, start_row - 1, -1):
output.append(matrix[row][start_col])
start_col += 1

return output
25 changes: 25 additions & 0 deletions valid-parentheses/gcount85.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Stack / Monotonic Stack
  • 설명: 이 코드는 괄호의 유효성을 검사하기 위해 스택을 이용하여 짝을 맞추는 방식으로 동작합니다. 스택을 활용한 괄호 매칭 문제는 대표적인 스택 패턴입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
# Approach
스택을 이용하여 닫는 괄호가 나올 때 스택 top과 비교하여 짝이 맞으면 pop합니다.
짝이 맞지 않으면 invalid, 최종적으로 스택이 비어있지 않으면 invalid 합니다.

# Complexity
s의 길이를 N이라고 할 때
- Time complexity: O(N)
- Space complexity: O(N)
"""


class Solution:
def isValid(self, s: str) -> bool:
stack = []
brackets = {"(": ")", "{": "}", "[": "]"}
for b in s:
if b in brackets:
stack.append(b)
continue
if not stack or brackets[stack.pop()] != b:
return False
if not stack:
return True
return False
Comment on lines +23 to +25
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

요렇게 짜시면 좀 더 Pythonic한 코드가 되지 않을까 생각해보았습니다. 별로 중요하지 않은 코멘트입니다.

Suggested change
if not stack:
return True
return False
return not stack

Loading