From 0e87ace063fdaa16bb21c65fa5b05afd0eb0d2cd Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 4 May 2025 00:45:50 +0900 Subject: [PATCH 1/8] Solve : Valid Parentheses --- valid-parentheses/printjin-gmailcom.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 valid-parentheses/printjin-gmailcom.py diff --git a/valid-parentheses/printjin-gmailcom.py b/valid-parentheses/printjin-gmailcom.py new file mode 100644 index 000000000..013c840c9 --- /dev/null +++ b/valid-parentheses/printjin-gmailcom.py @@ -0,0 +1,14 @@ +class Solution: + def isValid(self, s): + stack = [] + mapping = {')': '(', '}': '{', ']': '['} + for char in s: + if char in mapping.values(): + stack.append(char) + elif char in mapping: + if not stack or stack[-1] != mapping[char]: + return False + stack.pop() + else: + return False + return not stack From f2daef6d1cc89f33dc94c74d9d316b1bff9b433f Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 4 May 2025 00:50:28 +0900 Subject: [PATCH 2/8] Solve : Container With Most Water --- container-with-most-water/printjin-gmailcom.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 container-with-most-water/printjin-gmailcom.py diff --git a/container-with-most-water/printjin-gmailcom.py b/container-with-most-water/printjin-gmailcom.py new file mode 100644 index 000000000..e221e45e9 --- /dev/null +++ b/container-with-most-water/printjin-gmailcom.py @@ -0,0 +1,13 @@ +class Solution: + def maxArea(self, height): + left, right = 0, len(height) - 1 + max_area = 0 + while left < right: + h = min(height[left], height[right]) + w = right - left + max_area = max(max_area, h * w) + if height[left] < height[right]: + left += 1 + else: + right -= 1 + return max_area From 1f36003ed9d61c27511c35382d2c36eec308a14d Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 4 May 2025 00:51:38 +0900 Subject: [PATCH 3/8] Solve : Container With Most Water --- container-with-most-water/printjin-gmailcom.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/container-with-most-water/printjin-gmailcom.py b/container-with-most-water/printjin-gmailcom.py index e221e45e9..6fc26066d 100644 --- a/container-with-most-water/printjin-gmailcom.py +++ b/container-with-most-water/printjin-gmailcom.py @@ -1,13 +1,8 @@ class Solution: def maxArea(self, height): - left, right = 0, len(height) - 1 max_area = 0 - while left < right: - h = min(height[left], height[right]) - w = right - left - max_area = max(max_area, h * w) - if height[left] < height[right]: - left += 1 - else: - right -= 1 + for i in range(len(height)): + for j in range(i + 1, len(height)): + area = (j - i) * min(height[i], height[j]) + max_area = max(max_area, area) return max_area From 04aab794e10bd2e9c17f9985bec77945de2a6416 Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 4 May 2025 00:54:05 +0900 Subject: [PATCH 4/8] Solve : Container With Most Water --- container-with-most-water/printjin-gmailcom.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/container-with-most-water/printjin-gmailcom.py b/container-with-most-water/printjin-gmailcom.py index 6fc26066d..63c70ff09 100644 --- a/container-with-most-water/printjin-gmailcom.py +++ b/container-with-most-water/printjin-gmailcom.py @@ -1,8 +1,13 @@ class Solution: def maxArea(self, height): - max_area = 0 - for i in range(len(height)): - for j in range(i + 1, len(height)): - area = (j - i) * min(height[i], height[j]) - max_area = max(max_area, area) - return max_area + def divide_and_conquer(left, right): + if left >= right: + return 0 + min_height = min(height[left], height[right]) + area = min_height * (right - left) + return max( + area, + divide_and_conquer(left + 1, right), + divide_and_conquer(left, right - 1) + ) + return divide_and_conquer(0, len(height) - 1) From 7c69408aaa4dbce7946ffba3dbe62e8f4663987e Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 4 May 2025 00:54:36 +0900 Subject: [PATCH 5/8] Solve : Container With Most Water --- .../printjin-gmailcom.py | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/container-with-most-water/printjin-gmailcom.py b/container-with-most-water/printjin-gmailcom.py index 63c70ff09..e221e45e9 100644 --- a/container-with-most-water/printjin-gmailcom.py +++ b/container-with-most-water/printjin-gmailcom.py @@ -1,13 +1,13 @@ class Solution: def maxArea(self, height): - def divide_and_conquer(left, right): - if left >= right: - return 0 - min_height = min(height[left], height[right]) - area = min_height * (right - left) - return max( - area, - divide_and_conquer(left + 1, right), - divide_and_conquer(left, right - 1) - ) - return divide_and_conquer(0, len(height) - 1) + left, right = 0, len(height) - 1 + max_area = 0 + while left < right: + h = min(height[left], height[right]) + w = right - left + max_area = max(max_area, h * w) + if height[left] < height[right]: + left += 1 + else: + right -= 1 + return max_area From 4dc91523ff3f837eb98f159e9aac8be8624f5fba Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 4 May 2025 00:55:51 +0900 Subject: [PATCH 6/8] Solve : Design Add And Search Words Data Structure --- .../printjin-gmailcom.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 design-add-and-search-words-data-structure/printjin-gmailcom.py diff --git a/design-add-and-search-words-data-structure/printjin-gmailcom.py b/design-add-and-search-words-data-structure/printjin-gmailcom.py new file mode 100644 index 000000000..303823d5c --- /dev/null +++ b/design-add-and-search-words-data-structure/printjin-gmailcom.py @@ -0,0 +1,33 @@ +class TrieNode: + def __init__(self): + self.children = {} + self.is_end = False + +class WordDictionary: + + def __init__(self): + self.root = TrieNode() + + def addWord(self, word): + node = self.root + for ch in word: + if ch not in node.children: + node.children[ch] = TrieNode() + node = node.children[ch] + node.is_end = True + + def search(self, word): + def dfs(index, node): + if index == len(word): + return node.is_end + ch = word[index] + if ch == '.': + for child in node.children.values(): + if dfs(index + 1, child): + return True + return False + if ch in node.children: + return dfs(index + 1, node.children[ch]) + return False + + return dfs(0, self.root) From f6cb1d91f6daece7f3519db9df98014c19a31930 Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 4 May 2025 00:57:57 +0900 Subject: [PATCH 7/8] Solve : Longest Increasing Subsequence --- .../printjin-gmailcom.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 longest-increasing-subsequence/printjin-gmailcom.py diff --git a/longest-increasing-subsequence/printjin-gmailcom.py b/longest-increasing-subsequence/printjin-gmailcom.py new file mode 100644 index 000000000..883b2298c --- /dev/null +++ b/longest-increasing-subsequence/printjin-gmailcom.py @@ -0,0 +1,15 @@ +import bisect + +class Solution: + def lengthOfLIS(self, nums): + subsequence = [] + + for num in nums: + idx = bisect.bisect_left(subsequence, num) + + if idx == len(subsequence): + subsequence.append(num) + else: + subsequence[idx] = num + + return len(subsequence) From 7c7f32d1e5562e22ab7fb9c33755895c0c33caed Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 4 May 2025 01:00:07 +0900 Subject: [PATCH 8/8] Solve : Spiral Matrix --- spiral-matrix/printjin-gmailcom.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 spiral-matrix/printjin-gmailcom.py diff --git a/spiral-matrix/printjin-gmailcom.py b/spiral-matrix/printjin-gmailcom.py new file mode 100644 index 000000000..5080eae34 --- /dev/null +++ b/spiral-matrix/printjin-gmailcom.py @@ -0,0 +1,14 @@ +class Solution: + def spiralOrder(self, matrix): + result = [] + while matrix: + result += matrix.pop(0) + if matrix and matrix[0]: + for row in matrix: + result.append(row.pop()) + if matrix: + result += matrix.pop()[::-1] + if matrix and matrix[0]: + for row in matrix[::-1]: + result.append(row.pop(0)) + return result