diff --git a/container-with-most-water/daehyun99.py b/container-with-most-water/daehyun99.py new file mode 100644 index 0000000000..2a086277ee --- /dev/null +++ b/container-with-most-water/daehyun99.py @@ -0,0 +1,18 @@ +# Time: O(N) +# Space: O(1) +class Solution: + def maxArea(self, height: List[int]) -> int: + size = 0 + l, r = 0, len(height)-1 + length = len(height)-1 + + while l < r: + if height[l] < height[r]: + size = max(size, height[l] * length) + l += 1 + length -= 1 + else: + size = max(size, height[r] * length) + r -= 1 + length -= 1 + return size diff --git a/design-add-and-search-words-data-structure/daehyun99.py b/design-add-and-search-words-data-structure/daehyun99.py new file mode 100644 index 0000000000..6c105daf4a --- /dev/null +++ b/design-add-and-search-words-data-structure/daehyun99.py @@ -0,0 +1,39 @@ +class WordDictionary: + + def __init__(self): + self.root = {} + + def addWord(self, word: str) -> None: + pointer = self.root + for char in word: + if char in pointer: + pointer = pointer[char] + else: + pointer[char] = {} + pointer = pointer[char] + pointer["0"]={} + + def search(self, word: str) -> bool: + from collections import deque + pointer = self.root + que = deque() + que.append(pointer) + + for char in word: + for i in range(len(que)): + pointer = que.popleft() + if char in pointer: + que.append(pointer[char]) + elif char == ".": + for key in pointer.keys(): + que.append(pointer[key]) + while len(que) > 0: + pointer = que.popleft() + if "0" in pointer: + return True + return False + +# Your WordDictionary object will be instantiated and called as such: +# obj = WordDictionary() +# obj.addWord(word) +# param_2 = obj.search(word) diff --git a/longest-increasing-subsequence/daehyun99.py b/longest-increasing-subsequence/daehyun99.py new file mode 100644 index 0000000000..af00231962 --- /dev/null +++ b/longest-increasing-subsequence/daehyun99.py @@ -0,0 +1,11 @@ +# Time: O(N**2) +# Space: O(N) +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + counts = [1 for i in nums] + + for i in range(1, len(nums)): + for j in range(0, i): + if nums[j] < nums[i]: + counts[i] = max(counts[i], counts[j]+1) + return max(counts) diff --git a/spiral-matrix/daehyun99.py b/spiral-matrix/daehyun99.py new file mode 100644 index 0000000000..c9ec0e3e72 --- /dev/null +++ b/spiral-matrix/daehyun99.py @@ -0,0 +1,55 @@ +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + height = len(matrix) + weight = len(matrix[0]) + adding = [ + [0, 1], + [1, 0], + [0, -1], + [-1, 0] + ] + m = 0 + n = -1 + size = height * weight + count = 0 + + result = [] + + while count < size: + if 0 < weight and count < size: + m_add, n_add = adding[0] + for _ in range(weight): + m += m_add + n += n_add + result.append(matrix[m][n]) + count += 1 + + height -= 1 + if 0 < height and count < size: + m_add, n_add = adding[1] + for _ in range(height): + m += m_add + n += n_add + result.append(matrix[m][n]) + count += 1 + + weight -= 1 + if 0 < weight and count < size: + m_add, n_add = adding[2] + for _ in range(weight): + m += m_add + n += n_add + result.append(matrix[m][n]) + count += 1 + + height -= 1 + if 0 < height and count < size: + m_add, n_add = adding[3] + for _ in range(height): + m += m_add + n += n_add + result.append(matrix[m][n]) + count += 1 + + weight -= 1 + return result diff --git a/valid-parentheses/daehyun99.py b/valid-parentheses/daehyun99.py new file mode 100644 index 0000000000..084b06699a --- /dev/null +++ b/valid-parentheses/daehyun99.py @@ -0,0 +1,16 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + + map = {"()", "{}", "[]"} + + for char in s: + stack.append(char) + if len(stack) >= 2: + if ''.join(stack[-2:]) in map: + stack.pop() + stack.pop() + if len(stack) == 0: + return True + else: + return False