Skip to content

Commit 87559a1

Browse files
authored
Merge pull request #2193 from 8804who/main
[8804who] WEEK 06 solutions
2 parents 5bef82a + 3dc7e83 commit 87559a1

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
answer = 0
4+
start, end = 0, len(height)-1
5+
6+
while start <= end:
7+
area = (end-start)*(height[start] if height[start]<height[end] else height[end])
8+
if area > answer:
9+
answer = area
10+
if height[start] < height[end]:
11+
start += 1
12+
else:
13+
end -= 1
14+
return answer
15+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class WordDictionary:
2+
3+
def __init__(self):
4+
self.trie = {}
5+
6+
7+
def addWord(self, word: str) -> None:
8+
level = self.trie
9+
10+
for i in range(len(word)):
11+
if word[i] not in level:
12+
level[word[i]] = {}
13+
level = level[word[i]]
14+
level['end'] = True
15+
def search(self, word: str) -> bool:
16+
def dfs(idx, word, level):
17+
if idx == len(word):
18+
if 'end' in level:
19+
return True
20+
else:
21+
return False
22+
if word[idx] == '.':
23+
for i in level.keys():
24+
if i != 'end' and dfs(idx+1, word, level[i]):
25+
return True
26+
return False
27+
else:
28+
if word[idx] in level:
29+
return dfs(idx+1, word, level[word[idx]])
30+
else:
31+
return False
32+
return dfs(0, word, self.trie)
33+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from bisect import bisect_left
2+
class Solution:
3+
def lengthOfLIS(self, nums: List[int]) -> int:
4+
dp = [nums[0]]
5+
6+
for num in nums[1:]:
7+
if num>dp[-1]:
8+
dp.append(num)
9+
else:
10+
dp[bisect_left(dp, num)] = num
11+
12+
return len(dp)
13+

spiral-matrix/8804who.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
3+
answer = []
4+
5+
y_s, y_e = 0, len(matrix)-1
6+
x_s, x_e = 0, len(matrix[0])-1
7+
8+
while y_s <= y_e and x_s <= x_e:
9+
for i in range(x_s, x_e+1):
10+
answer.append(matrix[y_s][i])
11+
for i in range(y_s+1, y_e+1):
12+
answer.append(matrix[i][x_e])
13+
14+
if y_s==y_e or x_s==x_e:
15+
break
16+
17+
for i in range(x_e-1, x_s-1, -1):
18+
answer.append(matrix[y_e][i])
19+
for i in range(y_e-1, y_s, -1):
20+
answer.append(matrix[i][x_s])
21+
22+
y_s, y_e = y_s+1, y_e-1
23+
x_s, x_e = x_s+1, x_e-1
24+
25+
return answer
26+

valid-parentheses/8804who.py

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

0 commit comments

Comments
 (0)