diff --git a/container-with-most-water/i-mprovising.py b/container-with-most-water/i-mprovising.py new file mode 100644 index 000000000..d72fe61bc --- /dev/null +++ b/container-with-most-water/i-mprovising.py @@ -0,0 +1,22 @@ +""" +Time complexity O(n) +Space complexity O(1) + +Two pointer +""" + +class Solution: + def maxArea(self, height: List[int]) -> int: + s, e = 0, len(height) - 1 + area = 0 # max area + while s < e: + h = min(height[s], height[e]) + tmp = h * (e - s) # area at current iteration + area = max(area, tmp) + # move pointer + if height[s] < height[e]: + s += 1 + else: + e -= 1 + + return area diff --git a/design-add-and-search-words-data-structure/i-mprovising.py b/design-add-and-search-words-data-structure/i-mprovising.py new file mode 100644 index 000000000..bdec5a838 --- /dev/null +++ b/design-add-and-search-words-data-structure/i-mprovising.py @@ -0,0 +1,44 @@ +""" +Time complexity O(26^w) +Space complexity O(w) + +Trie, DFS +""" + +class Node: + def __init__(self, end=False): + self.children = {} # char : node + self.end = end + +class WordDictionary: + + def __init__(self): + self.root = Node(end=True) + + def addWord(self, word: str) -> None: + node = self.root + for ch in word: + if ch not in node.children: + node.children[ch] = Node() + node = node.children[ch] + node.end = True + + def search(self, word: str) -> bool: + return self.dfs(self.root, word) + + def dfs(self, start, word): + ch = word[0] + if ch == word: + if ch == '.': + return any([node.end for node in start.children.values()]) + elif ch in start.children: + if start.children[ch].end: + return True + return False + + if ch == '.': + return any([self.dfs(node, word[1:]) for c, node in start.children.items()]) + elif ch in start.children: + return self.dfs(start.children[ch], word[1:]) + else: + return False diff --git a/longest-increasing-subsequence/i-mprovising.py b/longest-increasing-subsequence/i-mprovising.py new file mode 100644 index 000000000..d6eb7a92b --- /dev/null +++ b/longest-increasing-subsequence/i-mprovising.py @@ -0,0 +1,29 @@ +""" +Time complexity O(n^2) +Space complexity O(n) + +Dynamic programming +""" + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + + dp = [(1, nums[0])] # sequence len, max num in sequence + + for i in range(1, len(nums)): + num = nums[i] + max_len = 1 + for j in range(i): + x, y = dp[j] + if y < num: + if max_len < x + 1: + max_len = x + 1 + dp.append((max_len, num)) + + # find max len + max_len = 0 + for x in dp: + if x[0] > max_len: + max_len = x[0] + + return max_len diff --git a/spiral-matrix/i-mprovising.py b/spiral-matrix/i-mprovising.py new file mode 100644 index 000000000..fdeb884e1 --- /dev/null +++ b/spiral-matrix/i-mprovising.py @@ -0,0 +1,60 @@ +""" +Time complexity O(m*n) + +단순구현 +""" + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + """ + 111 + 1M1 visited + 111 + """ + m, n = len(matrix), len(matrix[0]) + + board = [[0] * (n+2)] + for i in range(m): + tmp = [0] + matrix[i] + [0] + board.append(tmp) + board.append([0] * (n+2)) + + visited = [[True] * (n+1)] + for _ in range(m): + tmp = [True] + [False] * n + [True] + visited.append(tmp) + visited.append([True] * (n+1)) + + direction = 0 + x, y = 1, 1 + numbers = [] + + for _ in range(m * n): + numbers.append(board[x][y]) + visited[x][y] = True + + i, j = self.next_idx(direction, x, y) + if visited[i][j]: + direction = self.change_dir(direction) + x, y = self.next_idx(direction, x, y) + else: + x, y = i, j + + return numbers + + def next_idx(self, dir, x, y): + """ + 0 1 2 3 : R D L U + """ + if dir == 0: + y += 1 + elif dir == 1: + x += 1 + elif dir == 2: + y -= 1 + else: + x -= 1 + return x, y + + def change_dir(self, dir): + return (dir + 1) % 4 diff --git a/valid-parentheses/i-mprovising.py b/valid-parentheses/i-mprovising.py new file mode 100644 index 000000000..c5600822d --- /dev/null +++ b/valid-parentheses/i-mprovising.py @@ -0,0 +1,22 @@ +""" +Time complexity O(n) + +stack +""" +class Solution: + def isValid(self, s: str) -> bool: + open_brackets = ["(", "{", "["] + bracket_map = {"(":")", "{":"}", "[":"]"} + stack = [] + for char in s: + if char in open_brackets: + stack.append(char) + continue + if not stack: + return False + open_b = stack.pop() # last in open bracket + if bracket_map[open_b] != char: + return False + if stack: + return False + return True