File tree 1 file changed +31
-0
lines changed
1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+
3
+ # O(n^2) time | O(n) space
4
+ def diskStacking (disks ):
5
+ disks .sort (key = lambda disk : disks [2 ])
6
+ heights = [disks [2 ] for disk in disks ]
7
+ sequences = [None for disk in disks ]
8
+ maxHeightIndex = 0
9
+ for i in range (1 , len (disks )):
10
+ currentDisk = disks [i ]
11
+ for j in range (0 , i ):
12
+ otherDisk = disks [j ]
13
+ if areValidDimensions (otherDisk , currentDisk ):
14
+ if heights [i ] <= currentDisk [2 ] + heights [j ]:
15
+ heights [i ] = currentDisk [2 ] + heights [j ]
16
+ sequences [i ] = j
17
+ if heights [i ] >= heights [maxHeightIndex ]
18
+ maxHeightIndex = i
19
+ return buildSequence (disks , sequences , maxHeightIndex )
20
+
21
+
22
+ def areValidDimensions (other , current ):
23
+ return other [0 ] < current [0 ] and other [1 ] < current [1 ] and other [2 ] < current [2 ]
24
+
25
+
26
+ def buildSequence (array , sequences , currentIndex ):
27
+ sequence = []
28
+ while currentIndex is not None :
29
+ sequence .append (array [currentIndex ])
30
+ currentIndex = sequences [currentIndex ]
31
+ return list (reversed (sequence ))
You can’t perform that action at this time.
0 commit comments