@@ -128,7 +128,9 @@ class Solution {
128
128
class Solution {
129
129
public:
130
130
int maxHeight(vector<vector<int >>& cuboids) {
131
- for (auto& c : cuboids) sort(c.begin(), c.end());
131
+ for (auto& c : cuboids) {
132
+ sort(c.begin(), c.end());
133
+ }
132
134
sort(cuboids.begin(), cuboids.end());
133
135
int n = cuboids.size();
134
136
vector<int > f(n);
@@ -168,6 +170,33 @@ func maxHeight(cuboids [][]int) int {
168
170
}
169
171
```
170
172
173
+ ``` ts
174
+ function maxHeight(cuboids : number [][]): number {
175
+ for (const c of cuboids ) {
176
+ c .sort ((a , b ) => a - b );
177
+ }
178
+ cuboids .sort ((a , b ) => {
179
+ if (a [0 ] !== b [0 ]) {
180
+ return a [0 ] - b [0 ];
181
+ }
182
+ if (a [1 ] !== b [1 ]) {
183
+ return a [1 ] - b [1 ];
184
+ }
185
+ return a [2 ] - b [2 ];
186
+ });
187
+ const n = cuboids .length ;
188
+ const f = Array (n ).fill (0 );
189
+ for (let i = 0 ; i < n ; ++ i ) {
190
+ for (let j = 0 ; j < i ; ++ j ) {
191
+ const ok = cuboids [j ][1 ] <= cuboids [i ][1 ] && cuboids [j ][2 ] <= cuboids [i ][2 ];
192
+ if (ok ) f [i ] = Math .max (f [i ], f [j ]);
193
+ }
194
+ f [i ] += cuboids [i ][2 ];
195
+ }
196
+ return Math .max (... f );
197
+ }
198
+ ```
199
+
171
200
``` js
172
201
/**
173
202
* @param {number[][]} cuboids
@@ -178,12 +207,16 @@ var maxHeight = function (cuboids) {
178
207
c .sort ((a , b ) => a - b);
179
208
}
180
209
cuboids .sort ((a , b ) => {
181
- if (a[0 ] != b[0 ]) return a[0 ] - b[0 ];
182
- if (a[1 ] != b[1 ]) return a[1 ] - b[1 ];
210
+ if (a[0 ] !== b[0 ]) {
211
+ return a[0 ] - b[0 ];
212
+ }
213
+ if (a[1 ] !== b[1 ]) {
214
+ return a[1 ] - b[1 ];
215
+ }
183
216
return a[2 ] - b[2 ];
184
217
});
185
218
const n = cuboids .length ;
186
- const f = new Array (n).fill (0 );
219
+ const f = Array (n).fill (0 );
187
220
for (let i = 0 ; i < n; ++ i) {
188
221
for (let j = 0 ; j < i; ++ j) {
189
222
const ok = cuboids[j][1 ] <= cuboids[i][1 ] && cuboids[j][2 ] <= cuboids[i][2 ];
0 commit comments