Skip to content

Commit fbcf940

Browse files
Create HeightCuboidStack.java
1 parent da6551a commit fbcf940

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

HeightCuboidStack.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
// Variation of LIS Babbar Sheet Explaination saari dimensions ko particularly sort kro and pure whole ko sort kro last mein height ko add krte jao agr LIS pattern bn rha hai toh //
3+
public int maxHeight(int[][] cuboids) {
4+
int n = cuboids.length;
5+
for (int[] cuboid : cuboids) {
6+
Arrays.sort(cuboid);
7+
}
8+
Arrays.sort(cuboids, (a, b) -> {
9+
if (a[0] != b[0]) return a[0] - b[0];
10+
if (a[1] != b[1]) return a[1] - b[1];
11+
return a[2] - b[2];
12+
});
13+
14+
int[] dp = new int[n];
15+
int ans = 0;
16+
for (int i = 0; i < n; i++) {
17+
dp[i] = cuboids[i][2];
18+
for (int j = 0; j < i; j++) {
19+
if (cuboids[j][0] <= cuboids[i][0] &&
20+
cuboids[j][1] <= cuboids[i][1] &&
21+
cuboids[j][2] <= cuboids[i][2]) {
22+
dp[i] = Math.max(dp[i], dp[j] + cuboids[i][2]);
23+
}
24+
}
25+
ans = Math.max(ans, dp[i]);
26+
}
27+
return ans;
28+
}
29+
}

0 commit comments

Comments
 (0)