forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuilding Boxes.cpp
More file actions
27 lines (22 loc) · 898 Bytes
/
Building Boxes.cpp
File metadata and controls
27 lines (22 loc) · 898 Bytes
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
class Solution {
public:
int minimumBoxes(int n) {
// Find the biggest perfect pyramid
uint32_t h = cbrt((long)6*n);
uint32_t pyramid_box_num = h*(h+1)/2*(h+2)/3; // /6 is split to /2 and /3 to avoid long to save space
if ( pyramid_box_num > n ){
h--;
pyramid_box_num = h*(h+1)/2*(h+2)/3; // /6 is split to /2 and /3 to avoid long to save space
}
// Calculate how many floor-touching boxes in the pyramid
int pyramid_base_num = (h)*(h+1)/2;
// Get the number of boxes to be added to the perfect pyramid
n -= pyramid_box_num;
// Find how many floor-touching boxes are added
int z = sqrt(2*n);
int max_box_added = z*(z+1)/2;
if ( max_box_added < n)
z++;
return pyramid_base_num+z;
}
};