cswork / OAOpen practice workspace

RobloxOA #11

Make Towers Strictly Increasing or Decreasing

Estimated MediumStatement and editorial — judging is being prepared

Topics (AI-labelled)GreedyIn-place Arrays

给一个整数数组 towers。每次操作可对任一塔加 1 或减 1,cost 为 1。求把数组改成"严格递增"或"严格递减"的最小总操作数。返回较小者。

Example 1

Input:

towers = [1, 4, 3, 2]

Output:

4

Explanation: The given array can be transformed into a valid strictly monotonic sequence at the cost shown.

Example 2

Input:

towers = [5, 7, 9, 4, 11]

Output:

9

Explanation: The given array can be transformed into a valid strictly monotonic sequence at the cost shown.

Editorial

Loading solution…