Similar Problems
Similar Problems not available
Array Transformation - Leetcode Solution
Companies:
LeetCode: Array Transformation Leetcode Solution
Difficulty: Easy
Topics: array simulation
Problem Statement:
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:
- If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented.
- If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented.
- The first and last elements never change.
After some days, the array does not change. Return that final array.
Solution:
The problem requires transforming an array according to certain rules until it becomes stable and does not change any more. To approach this problem, we can start with a brute-force solution that performs the following steps:
- Keep iterating the transformation process until no changes are made to the array.
- In each iteration, apply the transformation rules to each element of the array.
- Use a flag to keep track of whether any changes were made in the current iteration.
- If no changes were made in an iteration, then the transformation process is complete.
Let's implement this algorithm in code:
class Solution { public: vector<int> transformArray(vector<int>& arr) { int n = arr.size(); vector<int> res = arr; bool changed = true; while(changed) { changed = false; for(int i=1;i<n-1;i++) { if(arr[i]<arr[i-1] && arr[i]<arr[i+1]) { res[i]++; changed = true; } else if(arr[i]>arr[i-1] && arr[i]>arr[i+1]) { res[i]--; changed = true; } } arr = res; } return res; } };
In this implementation, we start with a copy of the input array in variable res. We use a boolean variable changed to keep track of whether any changes were made in the current iteration. We then execute a while-loop that stops when no changes are made to the array in a particular iteration. In each iteration, we apply the transformation rules to each element and update the array. After each iteration, we update the variable arr with the new array, which becomes the input for the next iteration. Finally, we return the stable array.
The time complexity of this solution is O(n^2), since we might need to apply the transformation process to every element of the array in each iteration. This solution might not be efficient enough when the size of the input array is large. To optimize this solution, we can notice that:
- The transformation process only affects elements that are smaller than their neighbors or bigger than their neighbors.
- The array must eventually become stable, which means that no more than n/2 elements will need to be changed.
Based on these observations, we can implement a more optimized solution as follows:
class Solution { public: vector<int> transformArray(vector<int>& arr) { int n = arr.size(); vector<int> res = arr; bool changed = true; while(changed) { changed = false; int prev = arr[0]; for(int i=1;i<n-1;i++) { if(arr[i]<prev && arr[i]<arr[i+1]) { res[i]++; changed = true; } else if(arr[i]>prev && arr[i]>arr[i+1]) { res[i]--; changed = true; } prev = arr[i]; } arr = res; } return res; } };
In this implementation, we take advantage of the fact that the transformation process only affects elements that are smaller than their neighbors or bigger than their neighbors. We also notice that we only need to keep track of the previous element in the array to determine whether an element needs to be incremented or decremented. We iterate through the array only once, which gives us a time complexity of O(n). This solution is much more efficient than the previous solution.
Array Transformation Solution Code
1