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 @@ -30,3 +30,34 @@ var maxHeight = function (cuboids) {
30
30
return b [ 2 ] - a [ 2 ]
31
31
}
32
32
}
33
+
34
+ // another
35
+
36
+ /**
37
+ * @param {number[][] } cuboids
38
+ * @return {number }
39
+ */
40
+ var maxHeight = function ( cuboids ) {
41
+ cuboids . forEach ( ( cuboid ) => cuboid . sort ( ( a , b ) => a - b ) ) ;
42
+ cuboids . sort ( ( a , b ) => {
43
+ if ( a [ 0 ] !== b [ 0 ] ) return b [ 0 ] - a [ 0 ] ;
44
+ if ( a [ 1 ] !== b [ 1 ] ) return b [ 1 ] - a [ 1 ] ;
45
+ return b [ 2 ] - a [ 2 ] ;
46
+ } ) ;
47
+ const n = cuboids . length ;
48
+ const dp = Array ( n ) . fill ( 0 ) ;
49
+ let res = 0 ;
50
+ for ( let j = 0 ; j < n ; ++ j ) {
51
+ dp [ j ] = cuboids [ j ] [ 2 ] ;
52
+ for ( let i = 0 ; i < j ; ++ i ) {
53
+ if ( cuboids [ i ] [ 0 ] >= cuboids [ j ] [ 0 ]
54
+ && cuboids [ i ] [ 1 ] >= cuboids [ j ] [ 1 ]
55
+ && cuboids [ i ] [ 2 ] >= cuboids [ j ] [ 2 ]
56
+ ) {
57
+ dp [ j ] = Math . max ( dp [ j ] , dp [ i ] + cuboids [ j ] [ 2 ] ) ;
58
+ }
59
+ }
60
+ res = Math . max ( res , dp [ j ] ) ;
61
+ }
62
+ return res ;
63
+ } ;
You can’t perform that action at this time.
0 commit comments