Array Transformation using Bubble Sort Technique
Problem is here: https://leetcode.com/problems/array-transformation/
One way to solve is by using the same overall technique of a bubble sort algorithm, which in pseudo code would be:
Done = False
While Not Done
Done = True
For each index from 0..N-1
If A[index] > A[index+1]
Swap, and Mark Done = False
You can use the same technique for this problem. It is N^2, but since N = 100, it shouldn't be a problem. Code is below, cheers, ACC.
public class Solution
{
public IList<int> TransformArray(int[] arr)
{
int[] next = new int[arr.Length];
int[] prev = (int[])arr.Clone();
bool done = false;
while (!done)
{
next[0] = prev[0];
next[next.Length - 1] = prev[prev.Length - 1];
done = true;
for (int i = 1; i < next.Length - 1; i++)
{
next[i] = prev[i];
if (next[i] < prev[i - 1] && next[i] < prev[i + 1])
{
next[i]++;
done = false;
}
else if (next[i] > prev[i - 1] && next[i] > prev[i + 1])
{
next[i]--;
done = false;
}
}
prev = (int[])next.Clone();
}
return next.ToList<int>();
}
}
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.
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:
1 <= arr.length <= 100
1 <= arr[i] <= 100
One way to solve is by using the same overall technique of a bubble sort algorithm, which in pseudo code would be:
Done = False
While Not Done
Done = True
For each index from 0..N-1
If A[index] > A[index+1]
Swap, and Mark Done = False
You can use the same technique for this problem. It is N^2, but since N = 100, it shouldn't be a problem. Code is below, cheers, ACC.
public class Solution
{
public IList<int> TransformArray(int[] arr)
{
int[] next = new int[arr.Length];
int[] prev = (int[])arr.Clone();
bool done = false;
while (!done)
{
next[0] = prev[0];
next[next.Length - 1] = prev[prev.Length - 1];
done = true;
for (int i = 1; i < next.Length - 1; i++)
{
next[i] = prev[i];
if (next[i] < prev[i - 1] && next[i] < prev[i + 1])
{
next[i]++;
done = false;
}
else if (next[i] > prev[i - 1] && next[i] > prev[i + 1])
{
next[i]--;
done = false;
}
}
prev = (int[])next.Clone();
}
return next.ToList<int>();
}
}
Comments
Post a Comment