Skip to content

Commit 66de80b

Browse files
authored
Merge pull request #1871 from devyejin/main
[devyejin] WEEK 06 solutions
2 parents 7dc2303 + f0fed9f commit 66de80b

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def maxArea(self, height: List[int]) -> int:
6+
left, right = 0, len(height) - 1
7+
answer = 0
8+
9+
while left < right:
10+
answer = max(answer, (right - left) * min(height[left], height[right]))
11+
12+
if height[left] < height[right]:
13+
left += 1
14+
else:
15+
right -= 1
16+
17+
return answer
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class WordDictionary:
2+
3+
def __init__(self):
4+
self.root = {"$": False}
5+
6+
def addWord(self, word: str) -> None:
7+
node = self.root
8+
for ch in word:
9+
if ch not in node:
10+
node[ch] = {"$": False}
11+
node = node[ch]
12+
node["$"] = True
13+
14+
def search(self, word: str) -> bool:
15+
def dfs(node, idx):
16+
if idx == len(word):
17+
return node.get("$", False)
18+
19+
ch = word[idx]
20+
21+
if ch == ".":
22+
for key in node:
23+
if key == "$":
24+
continue
25+
if dfs(node[key], idx + 1):
26+
return True
27+
return False
28+
else:
29+
if ch in node:
30+
return dfs(node[ch], idx + 1)
31+
else:
32+
return False
33+
34+
return dfs(self.root, 0)
35+
36+
# Your WordDictionary object will be instantiated and called as such:
37+
# obj = WordDictionary()
38+
# obj.addWord(word)
39+
# param_2 = obj.search(word)
40+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
class Solution:
3+
def lengthOfLIS(self, nums: List[int]) -> int:
4+
def binary_search(target):
5+
left, right = 0, len(result)
6+
7+
while left < right:
8+
mid = (left + right) // 2
9+
if result[mid] < target:
10+
left = mid + 1
11+
else:
12+
right = mid
13+
return left
14+
15+
result = []
16+
17+
for num in nums:
18+
idx = binary_search(num)
19+
if idx == len(result):
20+
result.append(num)
21+
else:
22+
result[idx] = num
23+
24+
return len(result)
25+
26+
27+

spiral-matrix/devyejin.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
6+
dr = (0, 1, 0, -1)
7+
dc = (1, 0, -1, 0)
8+
r, c, d = 0, 0, 0
9+
10+
n, m = len(matrix), len(matrix[0])
11+
12+
result = []
13+
VISITED = None
14+
for _ in range(n * m):
15+
result.append(matrix[r][c])
16+
matrix[r][c] = VISITED
17+
18+
nr, nc = r + dr[d], c + dc[d]
19+
20+
if 0 <= nr < n and 0 <= nc < m and matrix[nr][nc] != VISITED:
21+
r, c = nr, nc
22+
continue
23+
24+
d = (d + 1) % 4
25+
r += dr[d]
26+
c += dc[d]
27+
28+
return result

valid-parentheses/devyejin.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
characters_dict = {')': '(', '}': '{', ']': '['}
4+
5+
stack = []
6+
for ch in s:
7+
if ch in characters_dict.values():
8+
stack.append(ch)
9+
elif ch in characters_dict:
10+
if not stack or stack[-1] != characters_dict[ch]:
11+
return False
12+
stack.pop()
13+
14+
return not stack
15+

0 commit comments

Comments
 (0)