diff --git a/container-with-most-water/Seoya0512.py b/container-with-most-water/Seoya0512.py new file mode 100644 index 0000000000..13a26d1d29 --- /dev/null +++ b/container-with-most-water/Seoya0512.py @@ -0,0 +1,26 @@ +''' +이중 For-loop을 사용하면 시간복잡도가 O(n^2)이 되므로 시간초과가 발생 +리트코드의 Hint를 참고해 Two Pointer를 사용하는 방식으로 시도했습니다. + +Time Complexity: O(n) +- While 루프를 사용해서 인덱스 i와 j가 만날 때까지 반복으로 진행하기에 O(n) + +Space Complexity: O(1) +- height 값과, max_area 값 모두 상수 공간을 사용 +''' +class Solution: + def maxArea(self, height: List[int]) -> int: + i, j = 0, len(height) - 1 + max_area = 0 + + while i < j: + area = min(height[i], height[j]) * (j-i) + max_area = max(area, max_area) + + if height[i] < height[j]: + i += 1 + else: + j -= 1 + + return max_area + diff --git a/design-add-and-search-words-data-structure/Seoya0512.py b/design-add-and-search-words-data-structure/Seoya0512.py new file mode 100644 index 0000000000..07a0add237 --- /dev/null +++ b/design-add-and-search-words-data-structure/Seoya0512.py @@ -0,0 +1,28 @@ +''' +이번 문제는 Trie 자료구조를 활용한 문제였습니다. +이를 파악하지 못해서 전체 탐색하는 구조를 만들었다 결국 또 도움을 받아 이해하는 것에 목표를 뒀습니다. +''' +class WordDictionary: + def __init__(self): + self.root = {"$": True} + + def addWord(self, word: str) -> None: + node = self.root + for ch in word: + if ch not in node: + node[ch] = {"$": False} + node = node[ch] + node["$"] = True + + def search(self, word: str) -> bool: + def dfs(node, idx): + if idx == len(word): + return node["$"] + ch = word[idx] + if ch in node: + return dfs(node[ch], idx + 1) + if ch == ".": + return any(dfs(node[k], idx + 1) for k in node if k != "$") + return False + return dfs(self.root, 0) + diff --git a/longest-increasing-subsequence/Seoya0512.py b/longest-increasing-subsequence/Seoya0512.py new file mode 100644 index 0000000000..14dc902d47 --- /dev/null +++ b/longest-increasing-subsequence/Seoya0512.py @@ -0,0 +1,12 @@ +''' +Dynamic Programming 문제로 누적합을 이용해 풀어보려 했으나 끝내 해결을 못해 참고했습니다. +''' +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + dp = [1] * len(nums) + for cur in range(1, len(nums)): + for prev in range(len(nums)): + if nums[prev] < nums[cur]: + dp[cur] = max(1+dp[prev], dp[cur]) + return max(dp) + diff --git a/spiral-matrix/Seoya0512.py b/spiral-matrix/Seoya0512.py new file mode 100644 index 0000000000..393e449d8a --- /dev/null +++ b/spiral-matrix/Seoya0512.py @@ -0,0 +1,38 @@ +''' +해당 문제를 확인하고 +상단 -> 우측 -> 하단 -> 좌측 방향으로 나선형으로 접근하는 것을 우선순위로 생각했습니다. +pop을 통해서 matrix의 행과 열을 제거하면서 접근하는 방식을 사용해 해결했습니다. + +이렇게 문제를 풀었을때 pop을 사용하여 행과 열을 제거하기 때문에 +비싼 계산 비용이 발생하는 부분을 고려하지 못했다는 점이 아쉽게 느껴지네요. + +Time Complexity: O(mxn) +- m: 행의 수만큼 접근 +- n: 열의 수만큼 접근 + +Space Complexity: O(mxn) +- result 리스트가 전체 요소를 저장 +''' +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + result = [] + idx = 0 # 처음 값 + while matrix: + result += matrix.pop(idx) + for i in range(len(matrix)): + if matrix[i]: + result.append(matrix[i].pop()) + + # 빈 행 제거 (중요!) + matrix = [row for row in matrix if row] + if not matrix: + break + + result += matrix.pop()[::-1] + + for j in range(len(matrix)-1, -1, -1): + if matrix[j]: + result.append(matrix[j].pop(0)) + + return result + diff --git a/valid-parentheses/Seoya0512.py b/valid-parentheses/Seoya0512.py new file mode 100644 index 0000000000..aeb7286597 --- /dev/null +++ b/valid-parentheses/Seoya0512.py @@ -0,0 +1,18 @@ +''' +20. Valid Parentheses +Stack을 사용하는 문제임을 파악하지 못해서 결국 알고달레님의 도움을 받았습니다. + +Time Complexity: O(n) +Space Complexity: O(n) +''' +class Solution: + def isValid(self, s: str) -> bool: + parens = {"(": ")", "{": "}", "[": "]"} + stack = [] + for val in s: + if val in parens: + stack.append(val) + else: + if not stack or val != parens[stack.pop()]: + return False + return not stack