-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.js
42 lines (34 loc) · 1.46 KB
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const fitsInOneBox = boxes => {
const sortedBoxes = [...boxes].sort((firstBox, secondBox) => {
return firstBox.l - secondBox.l;
});
return sortedBoxes.every(({ l: currentBoxL, w: currentBoxW, h: currentBoxH }, index, boxesList) => {
const { l: previousBoxL, w: previousBoxW, h: previousBoxH } = boxesList[index - 1] || {};
const isFirstBox = index === 0;
const canCurrentBoxContainPreviousOne =
currentBoxL > previousBoxL && currentBoxW > previousBoxW && currentBoxH > previousBoxH;
return isFirstBox || canCurrentBoxContainPreviousOne;
});
};
const fitsInOneBoxAlt = boxes => {
const sortedBoxes = [...boxes].sort((firstBox, secondBox) => {
return secondBox.l - firstBox.l;
});
return sortedBoxes.slice(1).every(({ l: currentBoxL, w: currentBoxW, h: currentBoxH }, index) => {
const { l: previousBoxL, w: previousBoxW, h: previousBoxH } = sortedBoxes[index] || {};
return currentBoxL < previousBoxL && currentBoxW < previousBoxW && currentBoxH < previousBoxH;
});
};
const fitsInOneBoxSome = boxes => {
const sortedBoxes = [...boxes].sort((firstBox, secondBox) => {
return firstBox.l - secondBox.l;
});
return !sortedBoxes.some((currentBox, index, sortedBoxesList) =>
sortedBoxesList
.slice(index + 1)
.some(remainBox =>
[remainBox.l > currentBox.l, remainBox.w > currentBox.w, remainBox.h > currentBox.h].includes(false)
)
);
};
export { fitsInOneBox, fitsInOneBoxAlt, fitsInOneBoxSome };