LeetCode 2525 - Categorize Box According to Criteria

The problem requires categorizing a box based on its dimensions and mass according to given criteria. We are given four integers: length, width, height, and mass. The output is a string that represents the category of the box: "Bulky", "Heavy", "Both", or "Neither".

LeetCode Problem 2525

Difficulty: 🟢 Easy
Topics: Math

Solution

Problem Understanding

The problem requires categorizing a box based on its dimensions and mass according to given criteria. We are given four integers: length, width, height, and mass. The output is a string that represents the category of the box: "Bulky", "Heavy", "Both", or "Neither".

A box is "Bulky" if any dimension is at least 104 units or if its volume, calculated as length * width * height, is at least 10^9. A box is "Heavy" if its mass is at least 100. The final category is determined by combining these two conditions: if both are true, the box is "Both"; if neither is true, it is "Neither"; otherwise, it is "Bulky" or "Heavy" depending on which condition holds.

The constraints ensure that all dimensions and mass are positive integers within reasonable ranges: dimensions are between 1 and 10^5, and mass is between 1 and 10^3. This implies that volume calculations may be large, but Python handles big integers natively. An important edge case is when the volume is exactly at the threshold 10^9, or when one of the dimensions is exactly 104, as these are boundary values that could cause off-by-one errors if not handled correctly.

Approaches

The naive or brute-force approach is straightforward: check each condition directly and combine results. This involves computing the volume and checking each dimension against 104, as well as comparing mass against 100. While simple, this is already efficient because we perform only a few arithmetic operations and comparisons.

There is no need for further optimization because all operations are O(1) in time; the only subtlety is handling potential integer overflow. In Python, integers are arbitrary-precision, so there is no overflow risk. In Go, careful attention must be paid to potential overflow when computing length * width * height. Using 64-bit integers ensures safety.

Approach Time Complexity Space Complexity Notes
Brute Force O(1) O(1) Directly evaluate each condition; constant time due to single calculation of volume and comparisons.
Optimal O(1) O(1) Same as brute-force because all operations are primitive and there is no iterative component; only careful comparison logic is needed.

Algorithm Walkthrough

  1. Compute the volume of the box as length * width * height. This is needed to determine whether the box meets the "Bulky" volume threshold.
  2. Check whether the box is "Bulky" by verifying if any dimension is greater than or equal to 104 or if the volume is at least 10^9. This is a logical OR condition.
  3. Check whether the box is "Heavy" by comparing its mass against 100.
  4. Combine the two boolean flags for "Bulky" and "Heavy" to determine the final category:
  • If both flags are true, return "Both".
  • If only "Bulky" is true, return "Bulky".
  • If only "Heavy" is true, return "Heavy".
  • If neither is true, return "Neither".

Why it works: Each condition is independent, and the logic for combining "Bulky" and "Heavy" categories is exhaustive, covering all possible cases. Therefore, this algorithm correctly classifies any valid input.

Python Solution

class Solution:
    def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
        volume = length * width * height
        is_bulky = length >= 104 or width >= 104 or height >= 104 or volume >= 10**9
        is_heavy = mass >= 100
        
        if is_bulky and is_heavy:
            return "Both"
        if is_bulky:
            return "Bulky"
        if is_heavy:
            return "Heavy"
        return "Neither"

The implementation first calculates the volume, then sets two boolean flags: is_bulky and is_heavy. These flags are combined using conditional statements to determine the correct category, matching the algorithm walkthrough precisely.

Go Solution

func categorizeBox(length int, width int, height int, mass int) string {
    volume := int64(length) * int64(width) * int64(height)
    isBulky := length >= 104 || width >= 104 || height >= 104 || volume >= 1e9
    isHeavy := mass >= 100
    
    if isBulky && isHeavy {
        return "Both"
    }
    if isBulky {
        return "Bulky"
    }
    if isHeavy {
        return "Heavy"
    }
    return "Neither"
}

In Go, the only difference is that volume is computed as int64 to avoid integer overflow, since length * width * height can exceed the maximum value of a standard int on some systems. The logical structure remains the same as the Python solution.

Worked Examples

Example 1: length = 1000, width = 35, height = 700, mass = 300

Variable Value
volume 1000 * 35 * 700 = 24,500,000
is_bulky False (all dimensions < 104 and volume < 10^9)
is_heavy True (mass >= 100)
Result "Heavy"

Example 2: length = 200, width = 50, height = 800, mass = 50

Variable Value
volume 200 * 50 * 800 = 8,000,000
is_bulky False
is_heavy False
Result "Neither"

Complexity Analysis

Measure Complexity Explanation
Time O(1) Only a constant number of arithmetic operations and comparisons are performed.
Space O(1) Only a few integer variables are used; no additional memory is allocated.

The algorithm is constant-time and space because it does not depend on input size iteratively; it performs fixed operations per input.

Test Cases

# Basic examples
assert Solution().categorizeBox(1000, 35, 700, 300) == "Heavy" # heavy but not bulky
assert Solution().categorizeBox(200, 50, 800, 50) == "Neither" # neither bulky nor heavy

# Boundary cases
assert Solution().categorizeBox(104, 1, 1, 1) == "Bulky" # exactly at dimension threshold
assert Solution().categorizeBox(1, 1, 10**9, 1) == "Bulky" # exactly at volume threshold
assert Solution().categorizeBox(104, 105, 106, 100) == "Both" # both thresholds met

# Edge mass cases
assert Solution().categorizeBox(1, 1, 1, 100) == "Heavy" # mass exactly threshold
assert Solution().categorizeBox(1, 1, 1, 99) == "Neither" # mass just below threshold

# Large volume case
assert Solution().categorizeBox(100000, 100000, 100000, 50) == "Bulky" # very large volume triggers bulky
Test Why
1000,35,700,300 Verifies heavy but not bulky
200,50,800,50 Verifies neither category
104,1,1,1 Dimension boundary test
1,1,10^9,1 Volume boundary test
104,105,106,100 Both bulky and heavy
1,1,1,100 Mass boundary test
1,1,1,99 Mass just below threshold
100000,100000,100000,50 Tests large volume computation

Edge Cases

First, the exact threshold values of dimensions (104) and volume (10^9) are critical because they test inclusive comparisons. Failing to use >= could misclassify these boxes. The implementation correctly uses >= to include these values.

Second, very large inputs can produce volumes up to 10^15, which can overflow 32-bit integers in some languages. The Python implementation handles this natively, while the Go solution explicitly casts to int64 to prevent overflow errors.

Third, mass exactly at the threshold 100 is an important edge case. The check mass >= 100 ensures that boxes with mass exactly 100 are classified as "Heavy" correctly, avoiding off-by-one errors.

These edge cases demonstrate that careful attention to boundaries and arithmetic overflow is essential for correct classification.