Skip to content

Commit 974c8d0

Browse files
committed
[main] Added maxArea
1 parent a03c7d5 commit 974c8d0

File tree

2 files changed

+54
-31
lines changed

2 files changed

+54
-31
lines changed

pythonProblems/leetcode/isMatch.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
class Solution:
22
def isMatch(self, s: str, p: str) -> bool:
3-
if '.' not in p and '*' not in p:
4-
if len(s) == len(p):
5-
return s == p
3+
4+
cache = {}
5+
6+
def dfs(i, j):
7+
8+
if (i, j) in cache:
9+
return cache[(i, j)]
10+
11+
if i >= len(s) and j >= len(p):
12+
return True
13+
if j >= len(p):
14+
return False
15+
16+
match = i < len(s) and (s[i] == p[j] or p[j] == '.')
17+
if (j + 1) < len(p) and p[j + 1] == '*':
18+
cache[(i, j)] = dfs(i, j + 2) or (match and dfs(i + 1, j))
19+
return cache[(i, j)]
20+
21+
if match:
22+
cache[(i, j)] = dfs(i + 1, j + 1)
23+
return cache[(i, j)]
24+
25+
cache[(i, j)] = False
626
return False
7-
i = 0
8-
j = 0
9-
while j < len(p) - 1 and i < len(s):
10-
if p[j + 1] == '*':
11-
## two decisions, whether to use or not to use
12-
if s.count(p[j]) == 0 and p[j] != '.':
13-
## choose not to use
14-
j += 2
15-
continue
16-
elif p[j] == s[i] or p[j] == '.':
17-
for k in range(i, len(s)):
18-
if s[k] == p[j] or p[j] == '.':
19-
i += 1
20-
else:
21-
break
22-
j += 2
23-
24-
elif p[j] == '.':
25-
j += 1
26-
else:
27-
if p[j] != s[i]:
28-
return False
29-
else:
30-
j += 1
31-
i += 1
32-
return p[j] == s[i] or p[j] == '.' if j < len(p) else True
27+
28+
return dfs(0, 0)
3329

3430

3531
if __name__ == '__main__':
3632
sol = Solution()
37-
print(sol.isMatch("mississippi"
38-
"mis*is*p*."
33+
print(sol.isMatch("aaa",
34+
"a*a"
35+
36+
3937
))

pythonProblems/leetcode/maxArea.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def maxArea(self, height: List[int]) -> int:
6+
7+
left_ = 0
8+
right_ = len(height) - 1
9+
max_water = 0
10+
11+
while left_ != right_:
12+
max_water = max(
13+
(right_ - left_) * min(height[left_], height[right_]), max_water)
14+
if height[left_] < height[right_]:
15+
left_ += 1
16+
elif height[right_] < height[left_]:
17+
right_ -= 1
18+
else:
19+
left_ += 1
20+
return max_water
21+
22+
23+
if __name__ == '__main__':
24+
sol = Solution()
25+
print(sol.maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]))

0 commit comments

Comments
 (0)