• Source
    1. import java.util.*;
    2.  
    3.  
    4. class Ideone{
    5.  
    6. public static void main(String[] args){
    7. Box[] boxArray = getInitialBoxDimensions();
    8. Arrays.sort(boxArray);
    9. System.out.println("Max height => " + longestHeightIncreasingSubseq(boxArray));
    10. }
    11.  
    12. private static int longestHeightIncreasingSubseq(Box[] boxes){
    13. int[] dp = new int[boxes.length];
    14. for(int i=0; i<dp.length; i++){
    15. dp[i] = boxes[i].height;
    16. }
    17.  
    18.  
    19. for(int i=1; i<boxes.length; i++){
    20. for(int j=0; j<i; j++){
    21. if(dp[i] < dp[j]+boxes[i].height && boxes[i].width < boxes[j].width && boxes[i].length < boxes[j].length){
    22. dp[i] = dp[j] + boxes[i].height;
    23. }
    24. }
    25. }
    26.  
    27. for(int i=0; i<boxes.length; i++){
    28. System.out.println("lis =>" + dp[i] + " height => "+boxes[i].height + " area => "+ (boxes[i].width * boxes[i].length));
    29. }
    30.  
    31. //Iterate over dp array and print max value
    32. int maxHeight = 0;
    33. for(int i=0; i<dp.length; i++){
    34. maxHeight = max(dp[i], maxHeight);
    35. }
    36.  
    37. return maxHeight;
    38. }
    39.  
    40. private static Box[] getInitialBoxDimensions(){
    41. Box[] array1 = getPossibleOrientations(4, 6, 7);
    42. Box[] array2 = getPossibleOrientations(1,2,3);
    43. Box[] array3 = getPossibleOrientations(4,5,6);
    44. Box[] array4 = getPossibleOrientations(10,12,32);
    45.  
    46. Box[] result = mergeTwoArrays(array1, array2);
    47. result = mergeTwoArrays(result, array3);
    48. result = mergeTwoArrays(result, array4);
    49.  
    50. return result;
    51. }
    52.  
    53. private static Box[] mergeTwoArrays(Box[] a1, Box[] a2){
    54. Box[] result = new Box[a1.length + a2.length];
    55. int tracker =0;
    56. for(int i=0; i<a1.length; i++){
    57. result[tracker++] = a1[i];
    58. }
    59.  
    60. for(int j=0;j<a2.length; j++){
    61. result[tracker++] = a2[j];
    62. }
    63. return result;
    64. }
    65.  
    66. private static Box[] getPossibleOrientations(int x, int y, int z){
    67. Box b1 = new Box(x,y,z);
    68. Box b2 = new Box(x,z,y);
    69. Box b3 = new Box(y,z,x);
    70. Box b4 = new Box(y,x,z);
    71. Box b5 = new Box(z,x,y);
    72. Box b6 = new Box(z,y,x);
    73. Box[] array = {b1, b2, b3, b4, b5, b6};
    74.  
    75. return array;
    76. }
    77.  
    78. private static int min(int a, int b){
    79. return a<b ? a : b;
    80. }
    81.  
    82. private static int max(int a, int b){
    83. return a > b ? a :b;
    84. }
    85.  
    86. private static class Box implements Comparable<Box>{
    87. int width;
    88. int length;//depth
    89. int height;
    90.  
    91. Box(int height, int length, int width){
    92. this.width = width;
    93. this.length = length;
    94. this.height = height;
    95. }
    96.  
    97. @Override
    98. public int compareTo(Box that){
    99. int areaOfThisObj = this.width * this.length;
    100. int areaOfThatObj = that.width * that.length;
    101. return areaOfThatObj - areaOfThisObj;
    102. }
    103. }
    104. }
    105.