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 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) 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) 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 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