diff --git a/container-with-most-water/ICE0208.py b/container-with-most-water/ICE0208.py new file mode 100644 index 0000000000..0f90688740 --- /dev/null +++ b/container-with-most-water/ICE0208.py @@ -0,0 +1,18 @@ +class Solution: + def maxArea(self, height: List[int]) -> int: + left, right = 0, len(height) - 1 + + most_water_size = 0 + while (left < right): + left_h, right_h = height[left], height[right] + + current_size = (right-left) * min(left_h, right_h) + most_water_size = max(most_water_size, current_size) + + if left_h < right_h: + left += 1 + else: + right -= 1 + + + return most_water_size diff --git a/design-add-and-search-words-data-structure/ICE0208.py b/design-add-and-search-words-data-structure/ICE0208.py new file mode 100644 index 0000000000..c1458c08c0 --- /dev/null +++ b/design-add-and-search-words-data-structure/ICE0208.py @@ -0,0 +1,47 @@ +class TrieNode: + def __init__(self): + self.children = {} + self.is_end = False + + +class WordDictionary: + + def __init__(self): + self.root = TrieNode() + + def addWord(self, word: str) -> None: + current = self.root + + for char in word: + if char not in current.children: + current.children[char] = TrieNode() + + current = current.children[char] + + current.is_end = True + + def search(self, word: str) -> bool: + def dfs(index: int, node: TrieNode) -> bool: + # 패턴을 모두 확인했을 때 실제 단어의 끝인지 확인한다. + if index == len(word): + return node.is_end + + char = word[index] + + # '.'은 현재 노드의 모든 자식 문자와 대응될 수 있다. + if char == ".": + for child in node.children.values(): + if dfs(index + 1, child): + return True + + return False + + # 일반 문자는 해당 자식 노드만 확인한다. + child = node.children.get(char) + + if child is None: + return False + + return dfs(index + 1, child) + + return dfs(0, self.root) diff --git a/longest-increasing-subsequence/ICE0208.py b/longest-increasing-subsequence/ICE0208.py new file mode 100644 index 0000000000..e5e52793b0 --- /dev/null +++ b/longest-increasing-subsequence/ICE0208.py @@ -0,0 +1,12 @@ +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + # dp[i] : i를 마지막으로 했을 때 가장긴 증가 부분수열 + dp = [0] * len(nums) + + for i in range(0, len(nums)): + dp[i] = 1 + for j in range(0, i): + if nums[j] < nums[i]: + dp[i] = max(dp[i], dp[j] + 1) + + return max(dp) diff --git a/spiral-matrix/ICE0208.py b/spiral-matrix/ICE0208.py new file mode 100644 index 0000000000..09144a90c7 --- /dev/null +++ b/spiral-matrix/ICE0208.py @@ -0,0 +1,45 @@ +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + VISIT_NUM = 10000 + + total = sum(map(lambda k: len(k), matrix)) + answer = [] + + def isVisit(i, j): + return matrix[i][j] == VISIT_NUM + + def setVisit(i, j): + matrix[i][j] = VISIT_NUM + + def inRange(i, j): + if not (0<=i stack = new ArrayDeque<>(); + Map mapping = new HashMap<>(); + mapping.put(')', '('); + mapping.put('}', '{'); + mapping.put(']', '['); + + for (Character c : s.toCharArray()) { + // 열린 괄호일 때, 스택에 push + if (c=='(' || c=='{' || c == '[') { + stack.push(c); + continue; + } + + // 닫힌 괄호일 때, 스택에서 pop한 뒤, 비교 + // 스택이 비어있으면 탈락 + if (stack.isEmpty()) { + return false; + } + Character popedC = stack.pop(); + Character target = mapping.get(c); + // 같은 종류의 괄호가 아니면 탈락 + if (!target.equals(popedC)) { + return false; + } + } + + // 마지막에 스택이 비어있어야 성공 + return stack.isEmpty(); + } +}