508. Array Transformation
Given an initial array arr, every day you produce a new array using the array of the previous day.
On the ith day, you perform the following operations on the array of day i - 1 to produce the array of day i:
- If an element is smaller than both its left and right neighbors, increment it.
- If an element is larger than both its left and right neighbors, decrement it.
- The first and last elements never change.
The transformation continues until the array stops changing. Return that final array.
Example 1:
Input: arr = [5, 1, 4, 3]
Output: [5, 3, 3, 3]
Explanation:
- Day 1: [5, 2, 3, 3]
- Day 2: [5, 3, 3, 3]
- No further changes can be made.
Example 2:
Input: arr = [2, 7, 1, 8, 4]
Output: [2, 4, 4, 4, 4]
Explanation:
- Day 1: [2, 6, 2, 7, 4]
- Day 2: [2, 5, 3, 6, 4]
- Day 3: [2, 4, 4, 5, 4]
- Day 4: [2, 4, 4, 4, 4]
- No further changes can be made.
Now Your Turn!
Pick the correct output for the given inputInput: arr = [9, 3, 5, 7, 2, 8]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 3 ≤ arr.length ≤ 100
- 1 ≤ arr[i] ≤ 100