2297. Jump Game VIII
Содержание
class Solution:
def minCost(self, nums: List[int], costs: List[int]) -> int:
n = len(nums)
g = defaultdict(list)
stk = []
for i in range(n - 1, -1, -1):
while stk and nums[stk[-1]] < nums[i]:
stk.pop()
if stk:
g[i].append(stk[-1])
stk.append(i)
stk = []
for i in range(n - 1, -1, -1):
while stk and nums[stk[-1]] >= nums[i]:
stk.pop()
if stk:
g[i].append(stk[-1])
stk.append(i)
f = [inf] * n
f[0] = 0
for i in range(n):
for j in g[i]:
f[j] = min(f[j], f[i] + costs[j])
return f[n - 1]