1243. Array Transformation

Given an initial array `arr`, every day you produce a new array using the array of the previous day.

On the `i`-th day, you do the following operations on the array of day `i-1` to produce the array of day `i`:

1. If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented.
2. If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented.
3. The first and last elements never change.

After some days, the array does not change. Return that final array.

Example 1:

```Input: arr = [6,2,3,4]
Output: [6,3,3,4]
Explanation:
On the first day, the array is changed from [6,2,3,4] to [6,3,3,4].
No more operations can be done to this array.
```

Example 2:

```Input: arr = [1,6,3,4,3,5]
Output: [1,4,4,4,4,5]
Explanation:
On the first day, the array is changed from [1,6,3,4,3,5] to [1,5,4,3,4,5].
On the second day, the array is changed from [1,5,4,3,4,5] to [1,4,4,4,4,5].
No more operations can be done to this array.
```

Constraints:

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

1243. Array Transformation
``````struct Solution;

impl Solution {
fn transform_array(mut array: Vec<i32>) -> Vec<i32> {
let n = array.len();
let mut temp = vec![0; n];
loop {
if Self::next(&mut array, &mut temp, n) {
break;
}
}
array
}
fn next(arr: &mut Vec<i32>, temp: &mut Vec<i32>, n: usize) -> bool {
for i in 1..n - 1 {
temp[i] = arr[i];
if arr[i] > arr[i - 1] && arr[i] > arr[i + 1] {
temp[i] -= 1;
}
if arr[i] < arr[i - 1] && arr[i] < arr[i + 1] {
temp[i] += 1;
}
}
let mut res = true;
for i in 1..n - 1 {
if arr[i] != temp[i] {
res = false;
arr[i] = temp[i];
}
}
res
}
}

#[test]
fn test() {
let arr = vec![6, 2, 3, 4];
let res = vec![6, 3, 3, 4];
assert_eq!(Solution::transform_array(arr), res);
let arr = vec![1, 6, 3, 4, 3, 5];
let res = vec![1, 4, 4, 4, 4, 5];
assert_eq!(Solution::transform_array(arr), res);
}
``````