diff --git a/container-with-most-water/sejineer.py b/container-with-most-water/sejineer.py new file mode 100644 index 000000000..37db7314b --- /dev/null +++ b/container-with-most-water/sejineer.py @@ -0,0 +1,17 @@ +""" +시간 복잡도: O(N) +공간 복잡도: O(1) +""" +class Solution: + def maxArea(self, height: List[int]) -> int: + result = 0 + start, end = 0, len(height) - 1 + + while start < end: + area = (end - start) * min(height[start], height[end]) + result = max(result, area) + if height[start] < height[end]: + start += 1 + else: + end -= 1 + return result diff --git a/design-add-and-search-words-data-structure/sejineer.py b/design-add-and-search-words-data-structure/sejineer.py new file mode 100644 index 000000000..0211a8afd --- /dev/null +++ b/design-add-and-search-words-data-structure/sejineer.py @@ -0,0 +1,26 @@ +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) + elif ch == ".": + return any(dfs(node[k], idx + 1) for k in node if k != "$") + else: + return False + return dfs(self.root, 0) diff --git a/longest-increasing-subsequence/sejineer.py b/longest-increasing-subsequence/sejineer.py new file mode 100644 index 000000000..986d26905 --- /dev/null +++ b/longest-increasing-subsequence/sejineer.py @@ -0,0 +1,16 @@ +""" +시간 복잡도: O(Nlog(N)) +공간 복잡도: O(N) +""" +from bisect import bisect_left + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + sub = [] + for num in nums: + index = bisect_left(sub, num) + if index == len(sub): + sub.append(num) + else: + sub[index] = num + return len(sub) diff --git a/spiral-matrix/sejineer.py b/spiral-matrix/sejineer.py new file mode 100644 index 000000000..ef16c95d4 --- /dev/null +++ b/spiral-matrix/sejineer.py @@ -0,0 +1,29 @@ +""" +시간 복잡도: O(n * m) +공간 복잡도: O(n * m) +""" +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + n, m = len(matrix), len(matrix[0]) + dx = [1, 0, -1, 0] + dy = [0, 1, 0, -1] + state = 0 + vis = [[False] * m for _ in range(n)] + + result = [matrix[0][0]] + vis[0][0] = True + x, y = 0, 0 + + while len(result) < n * m: + nx = x + dx[state % 4] + ny = y + dy[state % 4] + + if not (0 <= nx < m) or not (0 <= ny < n) or vis[ny][nx]: + state += 1 + continue + + vis[ny][nx] = True + result.append(matrix[ny][nx]) + x, y = nx, ny + + return result diff --git a/valid-parentheses/sejineer.py b/valid-parentheses/sejineer.py new file mode 100644 index 000000000..317a28953 --- /dev/null +++ b/valid-parentheses/sejineer.py @@ -0,0 +1,21 @@ +""" +시간 복잡도: O(N) +공간 복잡도: O(N) +""" +class Solution: + def isValid(self, s: str) -> bool: + pair = {'(': ')', '{': '}', '[': ']'} + stack = [] + for c in s: + if c in ('(', '{', '['): + stack.append(c) + else: + if stack: + cur = stack.pop() + if pair[cur] == c: + continue + else: + return False + else: + return False + return not stack