Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 45f7f99

Browse files
feat: add solutions to lc problems: No.1691,1699 (doocs#2299)
1 parent ebe805a commit 45f7f99

File tree

9 files changed

+337
-134
lines changed

9 files changed

+337
-134
lines changed

‎solution/1600-1699/1691.Maximum Height by Stacking Cuboids/README.md‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ class Solution {
128128
class Solution {
129129
public:
130130
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+
}
132134
sort(cuboids.begin(), cuboids.end());
133135
int n = cuboids.size();
134136
vector<int> f(n);
@@ -168,6 +170,33 @@ func maxHeight(cuboids [][]int) int {
168170
}
169171
```
170172

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+
171200
```js
172201
/**
173202
* @param {number[][]} cuboids
@@ -178,12 +207,16 @@ var maxHeight = function (cuboids) {
178207
c.sort((a, b) => a - b);
179208
}
180209
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+
}
183216
return a[2] - b[2];
184217
});
185218
const n = cuboids.length;
186-
const f = newArray(n).fill(0);
219+
const f = Array(n).fill(0);
187220
for (let i = 0; i < n; ++i) {
188221
for (let j = 0; j < i; ++j) {
189222
const ok = cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2];

‎solution/1600-1699/1691.Maximum Height by Stacking Cuboids/README_EN.md‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ class Solution {
124124
class Solution {
125125
public:
126126
int maxHeight(vector<vector<int>>& cuboids) {
127-
for (auto& c : cuboids) sort(c.begin(), c.end());
127+
for (auto& c : cuboids) {
128+
sort(c.begin(), c.end());
129+
}
128130
sort(cuboids.begin(), cuboids.end());
129131
int n = cuboids.size();
130132
vector<int> f(n);
@@ -164,6 +166,33 @@ func maxHeight(cuboids [][]int) int {
164166
}
165167
```
166168

169+
```ts
170+
function maxHeight(cuboids: number[][]): number {
171+
for (const c of cuboids) {
172+
c.sort((a, b) => a - b);
173+
}
174+
cuboids.sort((a, b) => {
175+
if (a[0] !== b[0]) {
176+
return a[0] - b[0];
177+
}
178+
if (a[1] !== b[1]) {
179+
return a[1] - b[1];
180+
}
181+
return a[2] - b[2];
182+
});
183+
const n = cuboids.length;
184+
const f = Array(n).fill(0);
185+
for (let i = 0; i < n; ++i) {
186+
for (let j = 0; j < i; ++j) {
187+
const ok = cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2];
188+
if (ok) f[i] = Math.max(f[i], f[j]);
189+
}
190+
f[i] += cuboids[i][2];
191+
}
192+
return Math.max(...f);
193+
}
194+
```
195+
167196
```js
168197
/**
169198
* @param {number[][]} cuboids
@@ -174,12 +203,16 @@ var maxHeight = function (cuboids) {
174203
c.sort((a, b) => a - b);
175204
}
176205
cuboids.sort((a, b) => {
177-
if (a[0] != b[0]) return a[0] - b[0];
178-
if (a[1] != b[1]) return a[1] - b[1];
206+
if (a[0] !== b[0]) {
207+
return a[0] - b[0];
208+
}
209+
if (a[1] !== b[1]) {
210+
return a[1] - b[1];
211+
}
179212
return a[2] - b[2];
180213
});
181214
const n = cuboids.length;
182-
const f = newArray(n).fill(0);
215+
const f = Array(n).fill(0);
183216
for (let i = 0; i < n; ++i) {
184217
for (let j = 0; j < i; ++j) {
185218
const ok = cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2];

‎solution/1600-1699/1691.Maximum Height by Stacking Cuboids/Solution.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
class Solution {
22
public:
33
int maxHeight(vector<vector<int>>& cuboids) {
4-
for (auto& c : cuboids) sort(c.begin(), c.end());
4+
for (auto& c : cuboids) {
5+
sort(c.begin(), c.end());
6+
}
57
sort(cuboids.begin(), cuboids.end());
68
int n = cuboids.size();
79
vector<int> f(n);

‎solution/1600-1699/1691.Maximum Height by Stacking Cuboids/Solution.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ var maxHeight = function (cuboids) {
77
c.sort((a, b) => a - b);
88
}
99
cuboids.sort((a, b) => {
10-
if (a[0] != b[0]) return a[0] - b[0];
11-
if (a[1] != b[1]) return a[1] - b[1];
10+
if (a[0] !== b[0]) {
11+
return a[0] - b[0];
12+
}
13+
if (a[1] !== b[1]) {
14+
return a[1] - b[1];
15+
}
1216
return a[2] - b[2];
1317
});
1418
const n = cuboids.length;
15-
const f = newArray(n).fill(0);
19+
const f = Array(n).fill(0);
1620
for (let i = 0; i < n; ++i) {
1721
for (let j = 0; j < i; ++j) {
1822
const ok = cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function maxHeight(cuboids: number[][]): number {
2+
for (const c of cuboids) {
3+
c.sort((a, b) => a - b);
4+
}
5+
cuboids.sort((a, b) => {
6+
if (a[0] !== b[0]) {
7+
return a[0] - b[0];
8+
}
9+
if (a[1] !== b[1]) {
10+
return a[1] - b[1];
11+
}
12+
return a[2] - b[2];
13+
});
14+
const n = cuboids.length;
15+
const f = Array(n).fill(0);
16+
for (let i = 0; i < n; ++i) {
17+
for (let j = 0; j < i; ++j) {
18+
const ok = cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2];
19+
if (ok) f[i] = Math.max(f[i], f[j]);
20+
}
21+
f[i] += cuboids[i][2];
22+
}
23+
return Math.max(...f);
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /