Show problems as
Classic problem #1243EasyArraySimulation

Array Transformation

Real-world version: Settling the Gauge Row
Tracked in this browser only
Read Array Transformation on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A row of gauges reads arr. Every round, all the gauges are updated at once from the readings the row held at the start of that round:

  • a gauge with a neighbour on each side rises by one when both neighbours read higher than it, and falls by one when both read lower;
  • otherwise it keeps its reading, and the gauges at the two ends never change.

Rounds continue until a round changes nothing. Return the row as it then stands.

Examples

Example 1

Input
arr = [1, 3, 2, 3]
Output
[1, 2, 3, 3]

In the first round the gauge reading 3 at position 1 has 1 and 2 either side, both lower, so it falls to 2, while the gauge reading 2 has 3 and 3 either side, both higher, so it rises to 3. The next round changes nothing.

Example 2

Input
arr = [5, 1, 5]
Output
[5, 5, 5]

The middle gauge rises by one each round while both neighbours stay higher, and it stops once it reaches them.

Example 3

Input
arr = [1, 2, 3]
Output
[1, 2, 3]

The middle gauge has one neighbour above and one below, so it never moves, and the two ends never move either.

Constraints

  • 3 <= arr.length <= 100
  • 1 <= arr[i] <= 100

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def settle_gauges(arr: list[int]) -> list[int]:
Java
public List<Integer> settleGauges(int[] arr)