Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2c216ec

Browse files
committed
Add solution #1739
1 parent e2cc4cc commit 2c216ec

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,512 LeetCode solutions in JavaScript
1+
# 1,513 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1340,6 +1340,7 @@
13401340
1736|[Latest Time by Replacing Hidden Digits](./solutions/1736-latest-time-by-replacing-hidden-digits.js)|Easy|
13411341
1737|[Change Minimum Characters to Satisfy One of Three Conditions](./solutions/1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js)|Medium|
13421342
1738|[Find Kth Largest XOR Coordinate Value](./solutions/1738-find-kth-largest-xor-coordinate-value.js)|Medium|
1343+
1739|[Building Boxes](./solutions/1739-building-boxes.js)|Hard|
13431344
1748|[Sum of Unique Elements](./solutions/1748-sum-of-unique-elements.js)|Easy|
13441345
1749|[Maximum Absolute Sum of Any Subarray](./solutions/1749-maximum-absolute-sum-of-any-subarray.js)|Medium|
13451346
1752|[Check if Array Is Sorted and Rotated](./solutions/1752-check-if-array-is-sorted-and-rotated.js)|Easy|

solutions/1739-building-boxes.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 1739. Building Boxes
3+
* https://leetcode.com/problems/building-boxes/
4+
* Difficulty: Hard
5+
*
6+
* You have a cubic storeroom where the width, length, and height of the room are all equal to n
7+
* units. You are asked to place n boxes in this room where each box is a cube of unit side
8+
* length. There are however some rules to placing the boxes:
9+
* - You can place the boxes anywhere on the floor.
10+
* - If box x is placed on top of the box y, then each side of the four vertical sides of the box
11+
* y must either be adjacent to another box or to a wall.
12+
*
13+
* Given an integer n, return the minimum possible number of boxes touching the floor.
14+
*/
15+
16+
/**
17+
* @param {number} n
18+
* @return {number}
19+
*/
20+
var minimumBoxes = function(n) {
21+
if (n < 4) return n;
22+
23+
let baseSize = 0;
24+
let totalBoxes = 0;
25+
const triangular = k => k * (k + 1) / 2;
26+
27+
while (totalBoxes + triangular(baseSize + 1) <= n) {
28+
baseSize++;
29+
totalBoxes += triangular(baseSize);
30+
}
31+
32+
const remaining = n - totalBoxes;
33+
let extraFloor = 0;
34+
35+
while (triangular(extraFloor) < remaining) extraFloor++;
36+
37+
return triangular(baseSize) + extraFloor;
38+
};

0 commit comments

Comments
 (0)