close
Note:
1. To maximized the gap, A[i-1] will be eithor floor (1) or ceiling (B[i-1]).
2. Focus on 2 possibilities with A[i] (floor 1 or ceiling B[i]) and their possible preceedings (previous sum with floor or ceiling)
def cost(B):
top, bottom = 0, 0
for i in range(1, len(B)):
t = max(top + abs(B[i] - B[i-1]), bottom + abs(B[i] - 1))
b = max(top + abs(B[i-1] -1), bottom)
top, bottom = t, b
return max(top, bottom)
全站熱搜