upd
This commit is contained in:
1
algorithms/labs/lab6/leetcode/1/link.txt
Normal file
1
algorithms/labs/lab6/leetcode/1/link.txt
Normal file
@@ -0,0 +1 @@
|
||||
https://leetcode.com/problems/regular-expression-matching/submissions/1769086671/?envType=problem-list-v2&envId=dynamic-programming
|
||||
14
algorithms/labs/lab6/leetcode/1/solution.py
Normal file
14
algorithms/labs/lab6/leetcode/1/solution.py
Normal file
@@ -0,0 +1,14 @@
|
||||
class Solution:
|
||||
def isMatch(self, s: str, p: str) -> bool:
|
||||
from functools import lru_cache
|
||||
|
||||
@lru_cache(None)
|
||||
def dp(i: int, j: int) -> bool:
|
||||
if j == len(p):
|
||||
return i == len(s)
|
||||
first = i < len(s) and (p[j] == s[i] or p[j] == ".")
|
||||
if j + 1 < len(p) and p[j + 1] == "*":
|
||||
return dp(i, j + 2) or (first and dp(i + 1, j))
|
||||
return first and dp(i + 1, j + 1)
|
||||
|
||||
return dp(0, 0)
|
||||
1
algorithms/labs/lab6/leetcode/2/link.txt
Normal file
1
algorithms/labs/lab6/leetcode/2/link.txt
Normal file
@@ -0,0 +1 @@
|
||||
https://leetcode.com/problems/longest-valid-parentheses/submissions/1769090768/?envType=problem-list-v2&envId=dynamic-programming
|
||||
15
algorithms/labs/lab6/leetcode/2/solution.py
Normal file
15
algorithms/labs/lab6/leetcode/2/solution.py
Normal file
@@ -0,0 +1,15 @@
|
||||
class Solution:
|
||||
def longestValidParentheses(self, s: str) -> int:
|
||||
n = len(s)
|
||||
dp = [0] * n
|
||||
best = 0
|
||||
for i in range(1, n):
|
||||
if s[i] == ")":
|
||||
if s[i - 1] == "(":
|
||||
dp[i] = (dp[i - 2] if i >= 2 else 0) + 2
|
||||
elif i - dp[i - 1] - 1 >= 0 and s[i - dp[i - 1] - 1] == "(":
|
||||
dp[i] = dp[i - 1] + 2
|
||||
if i - dp[i - 1] - 2 >= 0:
|
||||
dp[i] += dp[i - dp[i - 1] - 2]
|
||||
best = max(best, dp[i])
|
||||
return best
|
||||
1
algorithms/labs/lab6/leetcode/3/link.txt
Normal file
1
algorithms/labs/lab6/leetcode/3/link.txt
Normal file
@@ -0,0 +1 @@
|
||||
https://leetcode.com/problems/trapping-rain-water/?envType=problem-list-v2&envId=dynamic-programming
|
||||
20
algorithms/labs/lab6/leetcode/3/solution.py
Normal file
20
algorithms/labs/lab6/leetcode/3/solution.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def trap(self, height: List[int]) -> int:
|
||||
n = len(height)
|
||||
if n == 0:
|
||||
return 0
|
||||
left = [0] * n
|
||||
right = [0] * n
|
||||
left[0] = height[0]
|
||||
for i in range(1, n):
|
||||
left[i] = max(left[i - 1], height[i])
|
||||
right[-1] = height[-1]
|
||||
for i in range(n - 2, -1, -1):
|
||||
right[i] = max(right[i + 1], height[i])
|
||||
ans = 0
|
||||
for i in range(n):
|
||||
ans += min(left[i], right[i]) - height[i]
|
||||
return ans
|
||||
Reference in New Issue
Block a user