diff --git a/container-with-most-water/sungjinwi.py b/container-with-most-water/sungjinwi.py new file mode 100644 index 000000000..e2414dc28 --- /dev/null +++ b/container-with-most-water/sungjinwi.py @@ -0,0 +1,26 @@ +""" + 풀이 : + left, right 커서를 양 끝에 놓고 조건에 따라 반대편으로 이동시키며 물 양 비교 + 둘 중 더 높은 높이를 이동시키면 무조건 물의 양이 줄어들기 때문에 더 낮거나 같은 커서를 이동시키며 업데이트 + 둘이 만나면 비교 종료 + + + len(height) : N + TC : O(N) + l, r의 이동이 전체 height 개수만큼 일어나므로 + SC : O(1) +""" + +class Solution: + def maxArea(self, height: List[int]) -> int: + l = 0 + r = len(height) - 1 + max_area = 0 + while (l != r) : + cur_area = (r - l) * min(height[l], height[r]) + max_area = max(cur_area, max_area) + if (height[l] >= height[r]) : + r -= 1 + else : + l += 1 + return max_area diff --git a/longest-increasing-subsequence/sungjinwi.py b/longest-increasing-subsequence/sungjinwi.py new file mode 100644 index 000000000..f46d2a297 --- /dev/null +++ b/longest-increasing-subsequence/sungjinwi.py @@ -0,0 +1,25 @@ +""" + 풀이 : + 해당 인덱스의 숫자로 끝나는 LIS의 길이를 dp배열에 저장 + nums[i] 와 그 이후에 오는 nums[j]를 비교해 nums[j]가 크고 + i까지의 LIS 길이 + 1 > j까지의 LIS길이이면 j까지의 LIS길이 업데이트 + 업데이트 할 때마다 max길이와 비교해서 최대길이 업데이트 + + nums의 길이 N + TC : O(N^2) + for문의 개수를 살펴보면 N-1 + N-2 + ... + 1 = (N-1)N/2 이므로 N^2/2 -> O(N^2) + + SC : O(N) + dp의 길이는 nums의 길이에 비례하므로 +""" + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + dp = [1] * len(nums) + max_LIS = 1 + for i in range(len(nums) - 1) : + for j in range(i + 1, len(nums)) : + if nums[i] < nums[j] and dp[i] + 1 > dp[j] : + dp[j] = dp[i] + 1 + max_LIS = max(max_LIS, dp[j]) + return max_LIS diff --git a/spiral-matrix/sungjinwi.py b/spiral-matrix/sungjinwi.py new file mode 100644 index 000000000..0f3fc5ba7 --- /dev/null +++ b/spiral-matrix/sungjinwi.py @@ -0,0 +1,42 @@ +""" + 풀이 : + 오른쪽, 아래쪽으로 이동할 땐 각각 column과 row가 증가하므로 direction 1 + 왼쪽, 위쪽은 감소하므로 direction -1 + 로 설정하고 n_row 또는 n_col만큼 direction을 이동하면서 ans에 담아준다 + 각 for문이 끝날 때마다 n_row 또는 n_col을 감소시켜 주고 + 둘 중 하나가 0이 될 때까지 계속 진행 + + - col이 -1부터 시작해서 matrix[0][0]부터 append할 수 있도록 유의 + - n_cols, n_rows 둘 중 하나만 0이되도 끝남에 유의 + + TC : O(M * N) + matrix 전체를 한번씩 순회해야하므로 + + SC : O(1) + return할 리스트 외에는 추가적인 메모리 사용이 상수개(5개)이므로 +""" + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + ans = [] + n_rows = len(matrix) + n_cols = len(matrix[0]) + + row = 0 + col = -1 + direction = 1 + + while n_cols and n_rows : + for _ in range(n_cols): + col += direction + ans.append(matrix[row][col]) + n_rows -= 1 + + for _ in range(n_rows): + row += direction + ans.append(matrix[row][col]) + n_cols -= 1 + + direction *= -1 + + return ans diff --git a/valid-parentheses/sungjinwi.py b/valid-parentheses/sungjinwi.py new file mode 100644 index 000000000..9fa6f5663 --- /dev/null +++ b/valid-parentheses/sungjinwi.py @@ -0,0 +1,30 @@ +""" + 풀이 : + stack구조를 이용해서 구현 + 괄호의 페어를 딕셔너리의 key, value를 이용해 매치시킨다 + 여는 괄호를 만나면 stack에 push, 닫는 괄호를 만나면 stack의 마지막 괄호의 pair일 때 pop, 아니면 return False + 문자열 전체를 순회했을 때 stack에 남아있으면 안 닫힌 괄호가 있으므로 return False + + s의 길이 N + + TC : O(N) + s에 대해 한번 for문 + SC : O(N) + stack의 최대 크기는 s의 길이와 비례 +""" + +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + pair = dict(zip('({[',')}]')) + for paren in s : + if paren in pair : + stack.append(paren) + elif not stack : + return False + elif pair[stack.pop()] != paren : + return False + if not stack : + return True + else : + return False