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
  • 설명: 이 코드는 양 끝에서 시작하는 두 포인터를 이동시키며 최대 용량을 찾는 방식으로, Two Pointers 패턴에 속합니다. 효율적인 탐색을 위해 포인터를 조절하는 전략을 사용합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
# Approach
투포인터로 양 끝에서 물의 양을 계산해나갑니다.
더 낮은 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, Sliding Window
  • 설명: 이 코드는 상하좌우 경계값을 조절하며 행과 열을 순회하는 방식으로, 두 포인터 또는 슬라이딩 윈도우 패턴을 활용하여 배열을 탐색합니다.

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
23 changes: 23 additions & 0 deletions valid-parentheses/gcount85.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
# 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
return not stack
Loading