Skip to content

Commit 58b9ea4

Browse files
committed
【marscode 动态规划专题】Q2 Q11
1 parent bd4d8af commit 58b9ea4

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def solution(values: list) -> int:
2+
# PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
3+
# write code here
4+
5+
# values[i] + values[j] + i - j
6+
# -> values[j] - j + values[i] + i
7+
lmx = values[0]
8+
ans = 0
9+
for i in range(1, len(values)):
10+
ans = max(ans, lmx + values[i] - i)
11+
lmx = max(lmx, values[i] + i)
12+
return ans # Placeholder return
13+
14+
if __name__ == '__main__':
15+
print(solution(values=[8, 3, 5, 5, 6]) == 11)
16+
print(solution(values=[10, 4, 8, 7]) == 16)
17+
print(solution(values=[1, 2, 3, 4, 5]) == 8)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from functools import cache
2+
3+
4+
def solution(n, k, data):
5+
# Edit your code
6+
@cache
7+
def dfs(i: int, j: int) -> int:
8+
if i >= n: return 0
9+
ans = 0x3f3f3f3f
10+
if j > 0:
11+
# 不购买
12+
ans = min(ans, dfs(i + 1, j - 1))
13+
for x in range(1, k + 1):
14+
if j + x > k: break
15+
# 必须消耗一次
16+
ans = min(ans, dfs(i + 1, j + x - 1) + (data[i] * x))
17+
return ans
18+
19+
20+
res = dfs(0, 0)
21+
return res
22+
23+
if __name__ == "__main__":
24+
# Add your test cases here
25+
26+
print(solution(5, 2, [1, 2, 3, 3, 2]) == 9)

0 commit comments

Comments
 (0)