Building Boxes - Problem

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.

Input & Output

Example 1 — Small Pyramid
$ Input: n = 3
Output: 3
💡 Note: With 3 boxes, we need all 3 on the floor since we cannot stack them optimally. Any stacking would require perfect adjacent support which isn't possible with just 3 boxes.
Example 2 — Perfect Pyramid
$ Input: n = 10
Output: 6
💡 Note: We can build a pyramid with base layer of 6 boxes (arranged in triangular pattern 1+2+3), then add 3 boxes on second level (1+2), then 1 box on top. Total: 6+3+1 = 10 boxes.
Example 3 — Single Box
$ Input: n = 1
Output: 1
💡 Note: With only 1 box, it must be placed on the floor. Minimum floor boxes = 1.

Constraints

  • 1 ≤ n ≤ 109

Visualization

Tap to expand
Building Boxes: Minimize Floor UsageInput: 10 boxes📦×10Optimal Stacking12+3Base: 6 boxesResult6Floor boxesTotal boxes: 1 (top) + 3 (middle) + 6 (base) = 10 ✓Each stacked box has all 4 sides supported by adjacent boxesMinimum floor boxes: 6
Understanding the Visualization
1
Input
n=10 boxes to place
2
Stack Optimally
Build pyramid: 6 base + 3 middle + 1 top
3
Output
6 boxes touching floor
Key Takeaway
🎯 Key Insight: Stack boxes in pyramid formation to maximize height while minimizing base area
Asked in
Google 15 Amazon 12
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen