Problem

Given four integers length, width, height, and mass, representing the dimensions and mass of a box, respectively, return a string representing thecategory of the box.

The box is "Bulky" if:

  • Any of the dimensions of the box is greater or equal to 104.
  • Or, the volume of the box is greater or equal to 109.
    • If the mass of the box is greater or equal to 100, it is "Heavy".
    • If the box is both "Bulky" and "Heavy", then its category is "Both".
    • If the box is neither "Bulky" nor "Heavy", then its category is "Neither".
    • If the box is "Bulky" but not "Heavy", then its category is "Bulky".
    • If the box is "Heavy" but not "Bulky", then its category is "Heavy".

Note that the volume of the box is the product of its length, width and height.

Examples

Example 1

1
2
3
4
5
6
7
Input: length = 1000, width = 35, height = 700, mass = 300
Output: "Heavy"
Explanation: 
None of the dimensions of the box is greater or equal to 104. 
Its volume = 24500000 <= 10^9. So it cannot be categorized as "Bulky".
However mass >= 100, so the box is "Heavy".
Since the box is not "Bulky" but "Heavy", we return "Heavy".

Example 2

1
2
3
4
5
6
7
Input: length = 200, width = 50, height = 800, mass = 50
Output: "Neither"
Explanation: 
None of the dimensions of the box is greater or equal to 104.
Its volume = 8 * 106 <= 10^9. So it cannot be categorized as "Bulky".
Its mass is also less than 100, so it cannot be categorized as "Heavy" either. 
Since its neither of the two above categories, we return "Neither".

Constraints

  • 1 <= length, width, height <= 10^5
  • 1 <= mass <= 10^3

Solution

Method 1 – Condition Checking and Categorization

Intuition: We need to check a set of simple conditions on the box’s dimensions, volume, and mass to determine its category. The logic is direct: check for “Bulky” and “Heavy” independently, then combine the results as per the rules.

Approach:

  1. Calculate the volume as length × width × height.
  2. Check if any dimension ≥ 10^4 or volume ≥ 10^9 to determine if the box is “Bulky”.
  3. Check if mass ≥ 100 to determine if the box is “Heavy”.
  4. Return:
    • “Both” if both conditions are true.
    • “Bulky” if only bulky is true.
    • “Heavy” if only heavy is true.
    • “Neither” if neither is true.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    public String categorizeBox(int length, int width, int height, int mass) {
        boolean bulky = length >= 10000 || width >= 10000 || height >= 10000 || (long)length * width * height >= 1000000000L;
        boolean heavy = mass >= 100;
        if (bulky && heavy) return "Both";
        if (bulky) return "Bulky";
        if (heavy) return "Heavy";
        return "Neither";
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
    def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
        bulky = length >= 10**4 or width >= 10**4 or height >= 10**4 or length * width * height >= 10**9
        heavy = mass >= 100
        if bulky and heavy:
            return "Both"
        if bulky:
            return "Bulky"
        if heavy:
            return "Heavy"
        return "Neither"

Complexity

  • ⏰ Time complexity: O(1)
  • 🧺 Space complexity: O(1)