Given an unsorted array
, sort the given array. You are allowed to do only following operation on array.
flip(arr, i): Reverse array from 0 to i
Unlike a traditional sorting algorithm, which attempts to sort with the fewest comparisons possible, the goal is to sort the sequence in as few reversals as possible.
Example
Example 1:
Input: array = [6,11,10,12,7,23,20]
Output:[6,7,10,11,12,20,23]
Explanation:
flip(array, 5)
flip(array, 6)
flip(array, 0)
flip(array, 5)
flip(array, 1)
flip(array, 4)
flip(array, 1)
flip(array, 3)
flip(array, 1)
flip(array, 2)
Example 2:
Input: array = [4, 2, 3]
Output: [2, 3, 4]
Explanation:
flip(array, 2)
flip(array, 1)
Notice
You only call flip
function.
Don't allow to use any sort function or other sort methods.
Java:you can call FlipTool.flip(arr, i)
C++: you can call FlipTool::flip(arr, i)
Python2&3 you can call FlipTool.flip(arr, i)
思路:固定算法:
每次把curSize的最大的值flip到前面,然后flip回到最后,curSize--;
……a) Find index of the maximum element in arr[0..curr_szie-1]. Let the index be ‘mi’
……b) Call flip(arr, mi)
……c) Call flip(arr, curr_size-1)
See following video for visualization of the above algorithm.
http://www.youtube.com/embed/kk-_DDgoXfk
public class Solution {
/**
* @param array: an integer array
* @return: nothing
*/
public void pancakeSort(int[] A) {
if(A == null || A.length == 0) {
return;
}
int n = A.length;
for(int i = n-1; i >=0; i--) {
int j = findMaxIndex(A, i);
FlipTool.flip(A, j);
FlipTool.flip(A, i);
}
}
private int findMaxIndex(int[] A, int len) {
int maxIndex = 0;
int max = A[0];
for(int i = 0; i <= len; i++) {
if(A[i] > max) {
max = A[i];
maxIndex = i;
}
}
return maxIndex;
}
}
Given an array A
, we can perform a pancake flip: We choose some positive integer k <= A.length
, then reverse the order of the first k elements of A
. We want to perform zero or more pancake flips (doing them one after another in succession) to sort the array A
.
Return the k-values corresponding to a sequence of pancake flips that sort A
. Any valid answer that sorts the array within 10 * A.length
flips will be judged as correct.
Example 1:
Input: [3,2,4,1] Output: [4,2,4,3] Explanation: We perform 4 pancake flips, with k values 4, 2, 4, and 3. Starting state: A = [3, 2, 4, 1] After 1st flip (k=4): A = [1, 4, 2, 3] After 2nd flip (k=2): A = [4, 1, 2, 3] After 3rd flip (k=4): A = [3, 2, 1, 4] After 4th flip (k=3): A = [1, 2, 3, 4], which is sorted.
Example 2:
Input: [1,2,3] Output: [] Explanation: The input is already sorted, so there is no need to flip anything. Note that other answers, such as [3, 3], would also be accepted.
思路:每次找到当前len的最大index,flip到最前面,然后flip整个array。把最大丢到最后。
class Solution {
public List<Integer> pancakeSort(int[] A) {
List<Integer> list = new ArrayList<Integer>();
if(A == null || A.length == 0) {
return list;
}
int n = A.length;
for(int i = n - 1; i >= 0; i--) {
int j = findMaxIndex(A, i);
flip(A, j);
list.add(j + 1);
flip(A, i);
list.add(i + 1);
}
return list;
}
private int findMaxIndex(int[] A, int end) {
int maxIndex = 0;
int max = A[0];
for(int i = 1; i <= end; i++) {
if(A[i] > max) {
max = A[i];
maxIndex = i;
}
}
return maxIndex;
}
private void flip(int[] A, int end) {
int i = 0; int j = end;
while(i < j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
i++;
j--;
}
}
}