This commit is contained in:
nik
2025-10-01 22:55:09 +03:00
parent 4d0ece634b
commit 74e98c37c4
591 changed files with 20286 additions and 0 deletions

View File

@@ -0,0 +1 @@
https://leetcode.com/problems/regular-expression-matching/submissions/1769086671/?envType=problem-list-v2&envId=dynamic-programming

View 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)

View File

@@ -0,0 +1 @@
https://leetcode.com/problems/longest-valid-parentheses/submissions/1769090768/?envType=problem-list-v2&envId=dynamic-programming

View 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

View File

@@ -0,0 +1 @@
https://leetcode.com/problems/trapping-rain-water/?envType=problem-list-v2&envId=dynamic-programming

View 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