class Solution:
def arrayTransform(self, arr):
n = len(arr)
while True:
new_arr = arr[:]
for i in range(1, n - 1):
if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
new_arr[i] -= 1
elif arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
new_arr[i] += 1
if new_arr == arr:
break
arr = new_arr
return arr
class Solution {
public:
vector<int> arrayTransform(vector<int>& arr) {
int n = arr.size();
while (true) {
vector<int> new_arr = arr;
for (int i = 1; i < n - 1; ++i) {
if (arr[i] > arr[i-1] && arr[i] > arr[i+1]) {
new_arr[i]--;
} else if (arr[i] < arr[i-1] && arr[i] < arr[i+1]) {
new_arr[i]++;
}
}
if (new_arr == arr) break;
arr = new_arr;
}
return arr;
}
};
class Solution {
public int[] arrayTransform(int[] arr) {
int n = arr.length;
while (true) {
int[] newArr = arr.clone();
for (int i = 1; i < n - 1; i++) {
if (arr[i] > arr[i-1] && arr[i] > arr[i+1]) {
newArr[i]--;
} else if (arr[i] < arr[i-1] && arr[i] < arr[i+1]) {
newArr[i]++;
}
}
boolean changed = false;
for (int i = 0; i < n; i++) {
if (arr[i] != newArr[i]) {
changed = true;
break;
}
}
if (!changed) break;
arr = newArr;
}
return arr;
}
}
var arrayTransform = function(arr) {
let n = arr.length;
while (true) {
let newArr = arr.slice();
for (let i = 1; i < n - 1; i++) {
if (arr[i] > arr[i-1] && arr[i] > arr[i+1]) {
newArr[i]--;
} else if (arr[i] < arr[i-1] && arr[i] < arr[i+1]) {
newArr[i]++;
}
}
let changed = false;
for (let i = 0; i < n; i++) {
if (arr[i] !== newArr[i]) {
changed = true;
break;
}
}
if (!changed) break;
arr = newArr;
}
return arr;
};
Given an integer array arr
, you repeatedly apply the following transformation to it:
arr[i]
where 1 ≤ i < arr.length - 1
:arr[i]
is strictly greater than both its neighbors, decrease it by 1.arr[i]
is strictly less than both its neighbors, increase it by 1.
Repeat this process until the array no longer changes. Return the final state of arr
after all possible transformations.
Constraints:
The first step is to understand the transformation process: for each round, we look at every element (except the first and last), and based on its two neighbors, we decide whether to increment, decrement, or leave it alone. All transformations in a round must use the original values (not the updated ones from the same round).
A brute-force approach would be to simulate the process exactly as described: repeatedly scan the array, apply the rules, and repeat until nothing changes. However, we might wonder if there's a faster way—perhaps by recognizing that the process always converges to a stable state, and maybe we can reach it directly.
After closer inspection, the transformation is local (depends only on neighbors), and the process always stops after a finite number of steps because the array can't change forever. The brute-force simulation is acceptable for small arrays, but for larger ones, we want to avoid unnecessary work.
We will use a direct simulation approach, applying the transformation rules until the array stops changing. Here is how we do it:
i
from 1 to n - 2
:
arr[i]
is strictly greater than both arr[i-1]
and arr[i+1]
, decrement new_arr[i]
.arr[i]
is strictly less than both neighbors, increment new_arr[i]
.new_arr[i]
unchanged.new_arr
with the original arr
. If they are the same, stop and return arr
. Otherwise, repeat the process with new_arr
.
This approach is simple, easy to implement, and always finds the correct final state.
Let's use the example input: arr = [6, 2, 3, 4, 5, 6]
Array becomes: [6, 3, 4, 5, 6, 6]
Array becomes: [6, 4, 5, 6, 6, 6]
Array becomes: [6, 5, 6, 6, 6, 6]
Array becomes: [6, 6, 6, 6, 6, 6]
Final output: [6, 6, 6, 6, 6, 6]
Brute-force approach:
max_diff
is the difference between the largest and smallest element, as each element can only change by 1 per round.The Array Transformation problem is a classic example of simulating a local update process until stability is reached. By always using the original array for each round, we ensure correctness. The approach is simple, intuitive, and effective, requiring just a few lines of code and basic programming constructs. The key insight is that the process always converges, and the simulation directly implements the transformation as described.