diff --git a/container-with-most-water/gcount85.py b/container-with-most-water/gcount85.py new file mode 100644 index 0000000000..efd66495f3 --- /dev/null +++ b/container-with-most-water/gcount85.py @@ -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 diff --git a/longest-increasing-subsequence/gcount85.py b/longest-increasing-subsequence/gcount85.py new file mode 100644 index 0000000000..56df84e87b --- /dev/null +++ b/longest-increasing-subsequence/gcount85.py @@ -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 diff --git a/spiral-matrix/gcount85.py b/spiral-matrix/gcount85.py new file mode 100644 index 0000000000..556a5b1808 --- /dev/null +++ b/spiral-matrix/gcount85.py @@ -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 diff --git a/valid-parentheses/gcount85.py b/valid-parentheses/gcount85.py new file mode 100644 index 0000000000..e15095ca68 --- /dev/null +++ b/valid-parentheses/gcount85.py @@ -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