-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiskStacking.java
49 lines (46 loc) · 1.47 KB
/
DiskStacking.java
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
import java.util.*;
class Program {
// O(n^2) time | O(n) space
public static List<Integer[]> diskStacking(List<Integer[]> disks) {
// Write your code here.
// sort by heights
disks.sort((disk1, disk2) -> disk1[1].compareTo(disk2[2]));
int[] heights = new int[disks.size()];
for (int i = 0; i < disks.size(); i++) {
heights[i] = disks.get(i)[2];
}
// sequences are indexes of disks in the final stack
int[] sequences = new int[disks.size()];
for (int i = 0; i < disks.size(); i++) {
sequences[i] = Integer.MIN_VALUE;
}
int maxHeightIdx = 0;
for (int i = 1; i < disks.size(); i++) {
Integer[] disk = disks.get(i);
for (int j = 0; j < disks.size(); j++) {
Integer[] otherDisk = disks.get(j);
if (isSmaller(otherDisk, disk)) {
if (heights[i] <= disk[2] + heights[j]) {
heights[i] = disk[2] + heights[j]; // maximize total heights
sequences[i] = j;
}
}
}
if (heights[i] >= heights[maxHeightIdx]) {
maxHeightIdx = i;
}
}
return buildSequence(disks, sequences, maxHeightIdx);
}
private static boolean isSmaller(Integer[] disk, Integer[] otherDisk) {
return disk[0] < otherDisk[0] && disk[1] < otherDisk[1] && disk[2] < otherDisk[2];
}
private static List<Integer[]> buildSequence(List<Integer[]> disks, int[] sequences, int idx) {
List<Integer[]> sequence = new ArrayList<Integer[]>();
while (idx != Integer.MIN_VALUE) {
sequence.add(0, disks.get(idx));
idx = sequences[idx];
}
return sequence;
}
}