-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBoxStacking.java
More file actions
30 lines (29 loc) · 1.08 KB
/
BoxStacking.java
File metadata and controls
30 lines (29 loc) · 1.08 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
/*https://practice.geeksforgeeks.org/problems/box-stacking/1/*/
class Solution
{
public static int maxHeight(int[] height, int[] width, int[] length, int n)
{
int i = 0, len = 3*n, j = 0, result = 0;
Box[] maxHeap = new Box[len];
for (i = 0; i < n; ++i)
{
maxHeap[j++] = new Box(Math.max(length[i],width[i]),Math.min(length[i],width[i]),height[i]);
maxHeap[j++] = new Box(Math.max(length[i],height[i]),Math.min(length[i],height[i]),width[i]);
maxHeap[j++] = new Box(Math.max(width[i],height[i]),Math.min(width[i],height[i]),length[i]);
}
Arrays.sort(maxHeap);
int[] dp = new int[len];
for (i = 0; i < len; ++i)
dp[i] = maxHeap[i].h;
for (i = 0; i < len; ++i)
{
for (j = 0; j < i; ++j)
{
if (maxHeap[i].w < maxHeap[j].w && maxHeap[i].l < maxHeap[j].l && dp[i] <= dp[j]+maxHeap[i].h)
dp[i] = dp[j]+maxHeap[i].h;
result = Math.max(dp[i],result);
}
}
return result;
}
}