diff --git a/container-with-most-water/daiyongg-kim.py b/container-with-most-water/daiyongg-kim.py new file mode 100644 index 0000000000..92330faf3a --- /dev/null +++ b/container-with-most-water/daiyongg-kim.py @@ -0,0 +1,12 @@ +class Solution: + def maxArea(self, height: List[int]) -> int: + left = 0 + right = len(height) - 1 + max_area = 0 + while left < right: + max_area = max(max_area, min(height[left], height[right]) * (right - left)) + if height[left] < height[right]: + left += 1 + else: + right -= 1 + return max_area diff --git a/design-add-and-search-words-data-structure/daiyongg-kim.py b/design-add-and-search-words-data-structure/daiyongg-kim.py new file mode 100644 index 0000000000..c37c0eb174 --- /dev/null +++ b/design-add-and-search-words-data-structure/daiyongg-kim.py @@ -0,0 +1,42 @@ +class TrieNode(): + def __init__(self): + self.children = {} + self.word = False + +class WordDictionary: + + def __init__(self): + self.root = TrieNode() + + def addWord(self, word: str) -> None: + curr = self.root + for c in word: + if c not in curr.children: + curr.children[c] = TrieNode() + curr = curr.children[c] + curr.word = True + + def search(self, word: str) -> bool: + + def dfs(j, root): + curr = root + + for i in range(j, len(word)): + c = word[i] + + if c == ".": + for child in curr.children.values(): + if dfs(i+1, child): + return True + return False + else : + if c not in curr.children: + return False + curr = curr.children[c] + return curr.word + return dfs(0, self.root) + +# 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/daiyongg-kim.py b/longest-increasing-subsequence/daiyongg-kim.py new file mode 100644 index 0000000000..07bca42a81 --- /dev/null +++ b/longest-increasing-subsequence/daiyongg-kim.py @@ -0,0 +1,14 @@ +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + if not nums: + return 0 + + # 모든 위치에서 최소 길이는 1 (자기 자신) + dp = [1] * len(nums) + + for i in range(len(nums)): + for j in range(i): + if nums[i] > nums[j]: + dp[i] = max(dp[i], dp[j] + 1) + + return max(dp) diff --git a/spiral-matrix/daiyongg-kim.py b/spiral-matrix/daiyongg-kim.py new file mode 100644 index 0000000000..0b752821ef --- /dev/null +++ b/spiral-matrix/daiyongg-kim.py @@ -0,0 +1,27 @@ +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + res = [] + left, right = 0, len(matrix[0]) + top, bottom = 0, len(matrix) + + while left < right and top < bottom: + for i in range(left, right): + res.append(matrix[top][i]) + top += 1 + + for i in range(top, bottom): + res.append(matrix[i][right - 1]) + right -= 1 + + if not (left < right and top < bottom): + break + + for i in range(right -1, left -1, -1): + res.append(matrix[bottom - 1][i]) + bottom -= 1 + + for i in range(bottom - 1, top - 1, -1): + res.append(matrix[i][left]) + left += 1 + + return res diff --git a/valid-parentheses/daiyongg-kim.py b/valid-parentheses/daiyongg-kim.py new file mode 100644 index 0000000000..f5bf821b7c --- /dev/null +++ b/valid-parentheses/daiyongg-kim.py @@ -0,0 +1,22 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + + for c in s: + if c == '(' or c == '[' or c == '{': + stack.append(c) + else: + if not stack: + return False + + cur = stack.pop() + if cur == '(' and c != ')': + return False + if cur == '{' and c != '}': + return False + if cur == '[' and c != ']': + return False + if not stack: + return True + else: + return False