Skip to content

Commit 6d09629

Browse files
Merge pull request #1414 from printjin-gmailcom/main
[printjin-gmailcom] Week 06 Solution
2 parents 623e191 + 52805fa commit 6d09629

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maxArea(self, height):
3+
left, right = 0, len(height) - 1
4+
max_area = 0
5+
while left < right:
6+
h = min(height[left], height[right])
7+
w = right - left
8+
max_area = max(max_area, h * w)
9+
if height[left] < height[right]:
10+
left += 1
11+
else:
12+
right -= 1
13+
return max_area
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.children = {}
4+
self.is_end = False
5+
6+
class WordDictionary:
7+
8+
def __init__(self):
9+
self.root = TrieNode()
10+
11+
def addWord(self, word):
12+
node = self.root
13+
for ch in word:
14+
if ch not in node.children:
15+
node.children[ch] = TrieNode()
16+
node = node.children[ch]
17+
node.is_end = True
18+
19+
def search(self, word):
20+
def dfs(index, node):
21+
if index == len(word):
22+
return node.is_end
23+
ch = word[index]
24+
if ch == '.':
25+
for child in node.children.values():
26+
if dfs(index + 1, child):
27+
return True
28+
return False
29+
if ch in node.children:
30+
return dfs(index + 1, node.children[ch])
31+
return False
32+
33+
return dfs(0, self.root)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import bisect
2+
3+
class Solution:
4+
def lengthOfLIS(self, nums):
5+
subsequence = []
6+
7+
for num in nums:
8+
idx = bisect.bisect_left(subsequence, num)
9+
10+
if idx == len(subsequence):
11+
subsequence.append(num)
12+
else:
13+
subsequence[idx] = num
14+
15+
return len(subsequence)

spiral-matrix/printjin-gmailcom.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def spiralOrder(self, matrix):
3+
result = []
4+
while matrix:
5+
result += matrix.pop(0)
6+
if matrix and matrix[0]:
7+
for row in matrix:
8+
result.append(row.pop())
9+
if matrix:
10+
result += matrix.pop()[::-1]
11+
if matrix and matrix[0]:
12+
for row in matrix[::-1]:
13+
result.append(row.pop(0))
14+
return result
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def isValid(self, s):
3+
stack = []
4+
mapping = {')': '(', '}': '{', ']': '['}
5+
for char in s:
6+
if char in mapping.values():
7+
stack.append(char)
8+
elif char in mapping:
9+
if not stack or stack[-1] != mapping[char]:
10+
return False
11+
stack.pop()
12+
else:
13+
return False
14+
return not stack

0 commit comments

Comments
 (0)