Leetcode 1739. Building Boxes

Problem Explanation

You have a cubic storeroom where the width, length, and height of the room are all equal to n units. Your task is to place n boxes in this room, where each box is a cube with a side length of 1 unit. There are some rules for placing the boxes:

  1. You can place the boxes anywhere on the floor.
  2. If box x is placed on top of box y, then each side of the four vertical sides of box y must either be adjacent to another box or to a wall.

Given an integer n, you need to find the minimum possible number of boxes touching the floor.

Solution Approach

The problem can be solved using an iterative approach. We start by placing as many boxes on the floor as possible, and then proceed to build layers as needed. The minimum number of boxes touching the floor can be determined by maintaining a count of total boxes placed, while keeping track of how many boxes are on the current level, and the boxes touching the floor on the next level. We keep iterating until the total number of boxes placed is equal to or greater than the given value n.

Python solution

1class Solution:
2    def minimumBoxes(self, n: int) -> int:
3        nBoxes = 0
4        nextTouchings = 0
5        currLevelBoxes = 0
6
7        while nBoxes < n:
8            nextTouchings += 1
9            currLevelBoxes += nextTouchings
10            nBoxes += currLevelBoxes
11
12        if nBoxes == n:
13            return currLevelBoxes
14
15        nBoxes -= currLevelBoxes
16        currLevelBoxes -= nextTouchings
17        nextTouchings = 0
18
19        while nBoxes < n:
20            nextTouchings += 1
21            nBoxes += nextTouchings
22
23        return currLevelBoxes + nextTouchings

Java solution

1class Solution {
2    public int minimumBoxes(int n) {
3        int nBoxes = 0;
4        int nextTouchings = 0;
5        int currLevelBoxes = 0;
6
7        while (nBoxes < n) {
8            nextTouchings++;
9            currLevelBoxes += nextTouchings;
10            nBoxes += currLevelBoxes;
11        }
12
13        if (nBoxes == n) {
14            return currLevelBoxes;
15        }
16
17        nBoxes -= currLevelBoxes;
18        currLevelBoxes -= nextTouchings;
19        nextTouchings = 0;
20
21        while (nBoxes < n) {
22            nextTouchings++;
23            nBoxes += nextTouchings;
24        }
25
26        return currLevelBoxes + nextTouchings;
27    }
28}

JavaScript solution

1class Solution {
2    minimumBoxes(n) {
3        let nBoxes = 0;
4        let nextTouchings = 0;
5        let currLevelBoxes = 0;
6
7        while (nBoxes < n) {
8            nextTouchings++;
9            currLevelBoxes += nextTouchings;
10            nBoxes += currLevelBoxes;
11        }
12
13        if (nBoxes === n) {
14            return currLevelBoxes;
15        }
16
17        nBoxes -= currLevelBoxes;
18        currLevelBoxes -= nextTouchings;
19        nextTouchings = 0;
20
21        while (nBoxes < n) {
22            nextTouchings++;
23            nBoxes += nextTouchings;
24        }
25
26        return currLevelBoxes + nextTouchings;
27    }
28}

C++ solution

1class Solution {
2public:
3    int minimumBoxes(int n) {
4        int nBoxes = 0;
5        int nextTouchings = 0;
6        int currLevelBoxes = 0;
7
8        while (nBoxes < n) {
9            ++nextTouchings;
10            currLevelBoxes += nextTouchings;
11            nBoxes += currLevelBoxes;
12        }
13
14        if (nBoxes == n) {
15            return currLevelBoxes;
16        }
17
18        nBoxes -= currLevelBoxes;
19        currLevelBoxes -= nextTouchings;
20        nextTouchings = 0;
21
22        while (nBoxes < n) {
23            ++nextTouchings;
24            nBoxes += nextTouchings;
25        }
26
27        return currLevelBoxes + nextTouchings;
28    }
29};

C# solution

1public class Solution {
2    public int MinimumBoxes(int n) {
3        int nBoxes = 0;
4        int nextTouchings = 0;
5        int currLevelBoxes = 0;
6
7        while (nBoxes < n) {
8            nextTouchings++;
9            currLevelBoxes += nextTouchings;
10            nBoxes += currLevelBoxes;
11        }
12
13        if (nBoxes == n) {
14            return currLevelBoxes;
15        }
16
17        nBoxes -= currLevelBoxes;
18        currLevelBoxes -= nextTouchings;
19        nextTouchings = 0;
20
21        while (nBoxes < n) {
22            nextTouchings++;
23            nBoxes += nextTouchings;
24        }
25
26        return currLevelBoxes + nextTouchings;
27    }
28}

Explanation of Solution

Let's walk through the provided solution using an example. Suppose we have a storeroom with n = 4:

  1. Initially, the number of total boxes placed is 0, next touchings are 0, and the boxes at the current level are 0.

  2. For each iteration, we increment the next touchings and add it to the current level boxes.

  3. We then add the current level boxes to the overall number of boxes placed

  4. We repeat this until the total number of boxes placed is equal to or greater than the input n. In our example, after 2 iterations, we have placed 4 boxes, while the required value n is also 4.

  5. If the total boxes placed are equal to the input n, we simply return the number of boxes at the current level as the minimum number of boxes touching the floor.

  6. If the total boxes are greater than n, we rebuild the current level by removing boxes and then adding new touchings until the total boxes match the input n. In this case, we simply return the sum of current level boxes and next touchings.

In the example with n = 4, the function will return the minimum number of boxes touching the floor as 3.## Usage examples

Below are some example usages of the minimumBoxes function with a few different input values:

Python

1solution = Solution()
2print(solution.minimumBoxes(4))     # Output: 3
3print(solution.minimumBoxes(10))    # Output: 6
4print(solution.minimumBoxes(15))    # Output: 5

Java

1Solution solution = new Solution();
2System.out.println(solution.minimumBoxes(4));     // Output: 3
3System.out.println(solution.minimumBoxes(10));    // Output: 6
4System.out.println(solution.minimumBoxes(15));    // Output: 5

JavaScript

1const solution = new Solution();
2console.log(solution.minimumBoxes(4));     // Output: 3
3console.log(solution.minimumBoxes(10));    // Output: 6
4console.log(solution.minimumBoxes(15));    // Output: 5

C++

1Solution solution;
2std::cout << solution.minimumBoxes(4) << std::endl;     // Output: 3
3std::cout << solution.minimumBoxes(10) << std::endl;    // Output: 6
4std::cout << solution.minimumBoxes(15) << std::endl;    // Output: 5

C#

1Solution solution = new Solution();
2Console.WriteLine(solution.MinimumBoxes(4));     // Output: 3
3Console.WriteLine(solution.MinimumBoxes(10));    // Output: 6
4Console.WriteLine(solution.MinimumBoxes(15));    // Output: 5

In each of the usage examples, the minimumBoxes function is called with different input values for n. The output indicates the minimum number of boxes that must be touching the floor in the storeroom to satisfy the given conditions.


Got a question?ย Ask the Teaching Assistantย anything you don't understand.

Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.

โ†
โ†‘TA ๐Ÿ‘จโ€๐Ÿซ