Address https://leetcode-cn.com/contest/biweekly-contest-12/problems/array-transformation/
First of all, give you an initial array arr. Then, every day you have to generate a new array of arrays based on the previous day.
The resulting array on day i, i-1 is the first day of the array obtained by the following operations you:
If an element is less than its left and right neighbors, then the element is incremented by one.
If an element is greater than its left and right neighbors, then the element 1 is decremented.
The first and last elements never change.
After a while, you'll find an array will not change, return an array finally obtained.
Example 1 : Input: [ 6 , 2 , 3 , 4 ] Output: [ 6 , 3 , 3 , 4 ] Explanation: The first day, the array [ 6 , 2 , 3 , 4 ] to [ 6 , 3 , 3 , 4 ]. No longer the array more. Example 2 : Input: [ 1 , 6 , 3 , 4 , 3 , 5 ] Output: [1 , 4 , 4 , 4 , 4 , 5 ] Explanation: The first day, the array [ 1 , 6 , 3 , 4 , 3 , 5 ] to [ 1 , 5 , 4 , 3 , 4 , 5 ]. The next day, the array [ 1 , 5 , 4 , 3 , 4 , 5 ] to [ 1 , 4 , 4 , 4 ,4 , 5 ]. No longer the array more.
Algorithm 1
This question is just barely make out the optimization of the location, there are many
full simulation title meaning
each cycle opened a pig and ask array as long array for each element in turn to detect if they meet the meaning of the questions in the newly opened array +1 or -1 record
finally opened a new array added to the original array
if the array to open all zeros so that no changes can be out of the loop
1 class Solution { 2 public: 3 vector<int> transformArray(vector<int>& arr) { 4 if(arr.size() == 1 || arr.size() == 2) return arr; 5 6 while(1){ 7 vector<int> addvec(arr.size(),0); 8 for(int i = 1;i < arr.size()-1;i++){ 9 if(arr[i] > arr[i-1] && arr[i]>arr[i+1]) addvec[i] = -1; 10 else if(arr[i] < arr[i-1] && arr[i]<arr[i+1]) addvec[i] = 1; 11 } 12 int isAllZero = true; 13 for(int i = 0; i < addvec.size();i++){ 14 arr[i] += addvec[i]; 15 if(addvec[i] != 0) isAllZero =false; 16 } 17 if(isAllZero) break; 18 } 19 20 21 return arr; 22 } 23 };