Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
leetcode/algorithms/cpp/buildingBoxes/BuildingBoxes.cpp at master · fallleaves/leetcode · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
fallleaves
leetcode
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
leetcode
/
algorithms
/
cpp
/
buildingBoxes
/
BuildingBoxes.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
121 lines (101 loc) · 3.72 KB
master
Breadcrumbs
leetcode
/
algorithms
/
cpp
/
buildingBoxes
/
BuildingBoxes.cpp
Copy path
Top
File metadata and controls
Code
Blame
121 lines (101 loc) · 3.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
Source : https://leetcode.com/problems/building-boxes/
//
Author : Hao Chen
//
Date : 2021-04-09
/*
****************************************************************************************************
*
* You have a cubic storeroom where the width, length, and height of the room are all equal to n
* units. You are asked to place n boxes in this room where each box is a cube of unit side length.
* There are however some rules to placing the boxes:
*
* You can place the boxes anywhere on the floor.
* If box x is placed on top of the box y, then each side of the four vertical sides of the
* box y must either be adjacent to another box or to a wall.
*
* Given an integer n, return the minimum possible number of boxes touching the floor.
*
* Example 1:
*
* Input: n = 3
* Output: 3
* Explanation: The figure above is for the placement of the three boxes.
* These boxes are placed in the corner of the room, where the corner is on the left side.
*
* Example 2:
*
* Input: n = 4
* Output: 3
* Explanation: The figure above is for the placement of the four boxes.
* These boxes are placed in the corner of the room, where the corner is on the left side.
*
* Example 3:
*
* Input: n = 10
* Output: 6
* Explanation: The figure above is for the placement of the ten boxes.
* These boxes are placed in the corner of the room, where the corner is on the back side.
*
* Constraints:
*
* 1 <= n <= 10^9
*****************************************************************************************************
*/
/*
At first, let's build the perfect pyramid at the corner.
we can find the following sequence:
height cubes
1 1
2 1 + 2 = 3
3 1 + 2 + 3 = 6
4 1 + 2 + 3 + 4 = 10
5 1 + 2 + 3 + 4 + 5 = 15
total(height) = total(height - 1) + sum( from 1 to height )
sum ( from 1 to height) = (height * (height+1)) / 2
= height^2/2 + height/2
So,
total(height) = (1+2+...+height)/2 + ( 1^2 + 2^2 +...+ height^2 ) / 2
we know, Σn^2 = [n(n+1)(2n+1)]/6 (ref: https://brilliant.org/wiki/sum-of-n-n2-or-n3/)
So,
total(height) = (height * (height+1)) / 4 + (height(height+1)(2height+1))/12
= height * (height + 1) * (height + 2) / 6
for the rest cubes, we can place them like this
(10)
(6) (9)
(3) (5) (8)
(1) (2) (4) (7)
sum ( for 1 to n ) = n(n+1)/2
*/
class
Solution
{
private:
int
total
(
long
h){
return
h * (h+
1
) * (h+
2
) /
6
;
}
public:
int
minimumBoxes
(
int
n) {
//
find the maxiumn height which total(height) <= n
//
binary search
int
left =
1
, right =
pow
(
6l
*n,
1.0
/
3
) ;
while
(left <= right){
int
mid = left + (right - left) /
2
;
int
t =
total
(mid);
if
( t == n )
return
mid*(mid+
1l
)/
2
;
if
( t < n) left = mid +
1
;
else
right = mid -
1
;
}
int
height = right;
int
remind = n -
total
(height);
int
bottom = height * (height+
1l
)/
2
;
//
cout << "n=" << n << ", height=" << height <<
//
", bottom = " << bottom << ", remind=" << remind << endl;
//
find teh maxium h which sum(1..h) <= remind
//
binary search
left =
1
; right =
sqrt
(
2
*remind);
while
( left <= right) {
int
mid = left + (right - left)/
2
;
int
h = mid*(mid+
1
)/
2
;
if
( h == remind)
return
bottom + mid;
if
( h < remind) left = mid +
1
;
else
right = mid -
1
;
}
//
cout << "left=" << left << ", right=" << right << endl;
return
bottom + left;
}
};
You can’t perform that action at this time.