Building Boxes LeetCode solution in JavaScript

1
var minimumBoxes = function (n) {
2
//Binary search for the height of the biggest triangle I can create with n cubes available.
3
let lo = 1,
4
hi = n,
5
result;
6
let totalCubes = (x) => (x * (x + 1) * (x + 2)) / 6; //the total cubes of a triangle with height x
7
while (lo + 1 < hi) {
8
let mid = (lo + hi) >> 1;
9
if (totalCubes(mid) <= n) lo = mid;
10
else hi = mid;
11
}
12
let f = (x) => Math.floor(Math.sqrt(2 * x) + 1 / 2); // the i-th element of the sequence 1,2,2,3,3,3,4,4,4,4,5,...
13
result = (lo * (lo + 1)) / 2; // the base of the largest complete triangle 1+2+3+..+H
14
n -= totalCubes(lo); //remove the cubes of the complete triangle
15
return result + f(n); // the base of the largest+ the extra floor cubes
16
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0