#1739

Building Boxes

candidate master · 1375 · lc hard +32 · verified · 52.4% accepted · 314 likes · top 42%

Description

You have a cubic storeroom of side n. Place n unit-cube boxes so that: boxes may go anywhere on the floor; a box may be stacked on another only if all four vertical sides of the lower box touch either a wall or another box. Return the minimum number of floor-touching boxes.

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.

Code

1
2
3