unknown
python
3 years ago
706 B
10
Indexable
import math
def f(a):
best_modify_steps = {-1: 1, 0: 0, 1: 1}
for i in range(1, len(a)):
prev_best_modify_steps = best_modify_steps.copy()
diff = a[i] - a[i - 1]
for current_modify in [-1, 0, 1]:
best_modify_steps[current_modify] = min(
[
prev_best_modify_steps[prev_modify]
+ (0 if current_modify == 0 else 1)
if (diff + current_modify - prev_modify) >= 0
else math.inf
for prev_modify in [-1, 0, 1]
]
)
return min(best_modify_steps.values())
a = [3, 5, 3, 4, 8, 6]
print(f(a))
# [3, 5-1, 3+1, 4, 8-1, 6+1] => 4Editor is loading...