Stabilize Student Test Scores
Problem statement
The array A gives the test scores of students sitting in a row. Each day, every interior student compares their score with both immediate neighbors:
- If the score is strictly lower than both neighboring scores, increase it by1.
- If it is strictly higher than both neighboring scores, decrease it by1.
- Otherwise leave it unchanged.
All comparisons use the scores at the start of that day, and all changes happen simultaneously. The first and last scores never change. Repeat until a full day makes no changes, and return the final scores in the original order.
Function
solution(A: int[]) → int[]Examples
Example 1
A = [0,1,0,1,0]return = [0,0,0,0,0]After the first day, the scores are [0,0,1,0,0]. On the second day the center score drops to0, giving [0,0,0,0,0]. No later change is possible.
Example 2
A = [6,1,6]return = [6,6,6]The endpoints stay6. The middle score is below both neighbors and increases once per day for five days, then stops at6.
Constraints
- 1 <= A.length <= 1000.
- 0 <= A[i] <= 1000.