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:
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 <= 109
This problem asks for the minimum number of boxes touching the floor when placing n
unit cubes in a cubic room, subject to the constraint that any box placed on top of another must have its sides supported by adjacent boxes or walls. The optimal solution involves arranging the boxes in a stepped formation, minimizing the base layer.
The solution leverages a mathematical observation about the optimal arrangement: boxes are stacked in layers, forming a pyramid-like structure. Each layer adds a number of boxes equal to the layer's height (e.g., first layer: 1 box, second layer: 2 boxes, third layer: 3 boxes, and so on). This forms a triangular number sequence.
The algorithm efficiently finds the minimum number of boxes on the floor by:
Finding the Largest Complete Pyramid: It iteratively adds triangular numbers (1, 3, 6, 10, ...) until the sum exceeds n
(the total number of boxes). This identifies the largest complete pyramid that can be built.
Adding Remaining Boxes: Any remaining boxes (n
minus the sum of the complete pyramid) are added to the bottom layer, increasing the number of base boxes accordingly.
The key insight is that this greedy approach—building the largest possible pyramid first—always results in a minimum number of boxes on the floor.
Time Complexity: O(√n) - The loop iterating to find the largest complete pyramid runs approximately √n times, as triangular numbers grow quadratically. The subsequent loop to add remaining boxes also takes at most O(√n) time.
Space Complexity: O(1) - The solution uses only a few integer variables to store intermediate results; it doesn't use any data structures that scale with input size n
.
The provided code snippets implement this algorithm efficiently across different programming languages. The core logic remains the same:
Python:
class Solution:
def minimumBoxes(self, n: int) -> int:
s, k = 0, 1
while s + k * (k + 1) // 2 <= n:
s += k * (k + 1) // 2
k += 1
k -= 1
ans = k * (k + 1) // 2
k = 1
while s < n:
ans += 1
s += k
k += 1
return ans
Java:
class Solution {
public int minimumBoxes(int n) {
int s = 0, k = 1;
while (s + k * (k + 1) / 2 <= n) {
s += k * (k + 1) / 2;
++k;
}
--k;
int ans = k * (k + 1) / 2;
k = 1;
while (s < n) {
++ans;
s += k;
++k;
}
return ans;
}
}
C++:
class Solution {
public:
int minimumBoxes(int n) {
int s = 0, k = 1;
while (s + k * (k + 1) / 2 <= n) {
s += k * (k + 1) / 2;
++k;
}
--k;
int ans = k * (k + 1) / 2;
k = 1;
while (s < n) {
++ans;
s += k;
++k;
}
return ans;
}
};
Go:
func minimumBoxes(n int) int {
s, k := 0, 1
for s+k*(k+1)/2 <= n {
s += k * (k + 1) / 2
k++
}
k--
ans := k * (k + 1) / 2
k = 1
for s < n {
ans++
s += k
k++
}
return ans
}
All four code versions follow the same algorithmic steps, demonstrating the core solution's elegance and efficiency. They efficiently find the minimum number of boxes touching the floor for any given input n
.