Skip to content

Commit 94557d5

Browse files
committed
feat: add solution 1739. Building Boxes
1 parent 9696883 commit 94557d5

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# [1739. Building Boxes](https://leetcode.com/problems/building-boxes)
2+
3+
## Description
4+
5+
<div class="elfjS" data-track-load="description_content"><p>You have a cubic storeroom where the width, length, and height of the room are all equal to <code>n</code> units. You are asked to place <code>n</code> boxes in this room where each box is a cube of unit side length. There are however some rules to placing the boxes:</p>
6+
7+
<ul>
8+
<li>You can place the boxes anywhere on the floor.</li>
9+
<li>If box <code>x</code> is placed on top of the box <code>y</code>, then each side of the four vertical sides of the box <code>y</code> <strong>must</strong> either be adjacent to another box or to a wall.</li>
10+
</ul>
11+
12+
<p>Given an integer <code>n</code>, return<em> the <strong>minimum</strong> possible number of boxes touching the floor.</em></p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/3-boxes.png" style="width: 135px; height: 143px;"></p>
18+
19+
<pre><strong>Input:</strong> n = 3
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong> The figure above is for the placement of the three boxes.
22+
These boxes are placed in the corner of the room, where the corner is on the left side.
23+
</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/4-boxes.png" style="width: 135px; height: 179px;"></p>
28+
29+
<pre><strong>Input:</strong> n = 4
30+
<strong>Output:</strong> 3
31+
<strong>Explanation:</strong> The figure above is for the placement of the four boxes.
32+
These boxes are placed in the corner of the room, where the corner is on the left side.
33+
</pre>
34+
35+
<p><strong class="example">Example 3:</strong></p>
36+
37+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/10-boxes.png" style="width: 271px; height: 257px;"></p>
38+
39+
<pre><strong>Input:</strong> n = 10
40+
<strong>Output:</strong> 6
41+
<strong>Explanation:</strong> The figure above is for the placement of the ten boxes.
42+
These boxes are placed in the corner of the room, where the corner is on the back side.</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
49+
</ul>
50+
</div>
51+
52+
<p>&nbsp;</p>
53+
54+
## Solutions
55+
56+
**Solution: `Math`**
57+
58+
- Time complexity: <em>O(n<sup>1/2</sup>)</em>
59+
- Space complexity: <em>O(1)</em>
60+
61+
<p>&nbsp;</p>
62+
63+
### **JavaScript**
64+
65+
```js
66+
/**
67+
* @param {number} n
68+
* @return {number}
69+
*/
70+
const minimumBoxes = function (n) {
71+
let boxes = 0;
72+
let level = 0;
73+
let currentLevelBoxes = 0;
74+
75+
while (boxes < n) {
76+
level += 1;
77+
currentLevelBoxes += level;
78+
boxes += currentLevelBoxes;
79+
}
80+
81+
if (boxes === n) return currentLevelBoxes;
82+
83+
boxes -= currentLevelBoxes;
84+
currentLevelBoxes -= level;
85+
level = 0;
86+
87+
while (boxes < n) {
88+
level += 1;
89+
boxes += level;
90+
}
91+
92+
return currentLevelBoxes + level;
93+
};
94+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
const minimumBoxes = function (n) {
6+
let boxes = 0;
7+
let level = 0;
8+
let currentLevelBoxes = 0;
9+
10+
while (boxes < n) {
11+
level += 1;
12+
currentLevelBoxes += level;
13+
boxes += currentLevelBoxes;
14+
}
15+
16+
if (boxes === n) return currentLevelBoxes;
17+
18+
boxes -= currentLevelBoxes;
19+
currentLevelBoxes -= level;
20+
level = 0;
21+
22+
while (boxes < n) {
23+
level += 1;
24+
boxes += level;
25+
}
26+
27+
return currentLevelBoxes + level;
28+
};

0 commit comments

Comments
 (0)