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

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

View File

@@ -0,0 +1,29 @@
def thief_knapsack(weights, values, M, K):
cap = M * K
n = len(weights)
dp = [0] * (cap + 1)
take = [[False] * (cap + 1) for _ in range(n)]
for i in range(n):
w = weights[i]
v = values[i]
for c in range(cap, w - 1, -1):
if dp[c - w] + v > dp[c]:
dp[c] = dp[c - w] + v
take[i][c] = True
c = cap
items = []
for i in range(n - 1, -1, -1):
if take[i][c]:
items.append(i)
c -= weights[i]
items.reverse()
return dp[cap], items
if __name__ == "__main__":
weights = [2, 3, 4, 5, 9, 7, 3]
values = [3, 4, 5, 8, 10, 7, 6]
M = 2
K = 10
best, items = thief_knapsack(weights, values, M, K)
print(best, items)

View File

@@ -0,0 +1,35 @@
def matrix_chain_order(p):
n = len(p) - 1
m = [[0] * n for _ in range(n)]
s = [[0] * n for _ in range(n)]
for L in range(2, n + 1):
for i in range(n - L + 1):
j = i + L - 1
m[i][j] = 10**18
for k in range(i, j):
q = m[i][k] + m[k + 1][j] + p[i] * p[k + 1] * p[j + 1]
if q < m[i][j]:
m[i][j] = q
s[i][j] = k
return m, s
def build_parenthesization(s, i, j, names):
if i == j:
return names[i]
k = s[i][j]
return (
"("
+ build_parenthesization(s, i, k, names)
+ build_parenthesization(s, k + 1, j, names)
+ ")"
)
if __name__ == "__main__":
p = [30, 35, 15, 5, 10, 20, 25]
names = ["A", "B", "C", "D", "E", "F"][: len(p) - 1]
m, s = matrix_chain_order(p)
expr = build_parenthesization(s, 0, len(p) - 2, names)
print(m[0][len(p) - 2])
print(expr)

View File

@@ -0,0 +1,29 @@
def longest_increasing_run(a):
if not a:
return 0, -1, -1
best_len = 1
best_l = 0
best_r = 0
cur_len = 1
cur_l = 0
for i in range(1, len(a)):
if a[i] > a[i - 1]:
cur_len += 1
else:
if cur_len > best_len:
best_len = cur_len
best_l = cur_l
best_r = i - 1
cur_len = 1
cur_l = i
if cur_len > best_len:
best_len = cur_len
best_l = cur_l
best_r = len(a) - 1
return best_len, best_l, best_r
if __name__ == "__main__":
a = [1, 2, 3, -1, 0, 1, 1, 2, 3, 4, -5]
length, l, r = longest_increasing_run(a)
print(length, l, r)

Binary file not shown.

Binary file not shown.

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

Binary file not shown.

Binary file not shown.