-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPancakeSorting.java
More file actions
53 lines (43 loc) · 1.23 KB
/
PancakeSorting.java
File metadata and controls
53 lines (43 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package array;
import java.util.ArrayList;
import java.util.List;
/**
* Description: https://leetcode.com/problems/pancake-sorting
* Difficulty: Medium
* Time complexity: O(n^2)
* Space complexity: O(n)
*/
public class PancakeSorting {
public List<Integer> pancakeSort(int[] arr) {
List<Integer> flips = new ArrayList<>();
for (int current = arr.length; current > 0; current--) {
int index = find(arr, current);
if (index == current - 1) continue; // already in place
if (index != 0) {
// flip to the head
flips.add(index + 1);
flip(arr, index + 1);
}
// flip to the tail
flips.add(current);
flip(arr, current);
}
return flips;
}
private void flip(int[] arr, int index) {
for (int i = 0; i < index / 2; i++) {
swap(arr, i, index - i - 1);
}
}
private void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
private int find(int[] arr, int val) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == val) return i;
}
return -1;
}
}