Given four integers length, width, height, and mass, representing the dimensions and mass of a box, respectively, return a string representing the category of the box.
"Bulky" if:
104.109.100, it is "Heavy"."Bulky" and "Heavy", then its category is "Both"."Bulky" nor "Heavy", then its category is "Neither"."Bulky" but not "Heavy", then its category is "Bulky"."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.
Example 1:
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 <= 109. 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:
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 <= 109. 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 <= 1051 <= mass <= 103Problem Overview: You receive the length, width, height, and mass of a box. The task is to classify it as Bulky, Heavy, Both, or Neither based on size and weight rules. A box is bulky if any dimension is at least 10,000 or its volume is at least 1,000,000,000. It is heavy if its mass is at least 100.
Approach 1: Simple Conditional Checks (O(1) time, O(1) space)
This approach directly translates the problem rules into conditional checks. First compute the box volume using length * width * height. Then determine two boolean flags: one for the bulky condition (dimension ≥ 10000 or volume ≥ 1e9) and another for the heavy condition (mass ≥ 100). Once those flags are known, use a small set of if conditions to return "Both", "Bulky", "Heavy", or "Neither". Since only a few arithmetic operations and comparisons are performed, the algorithm runs in constant time and uses constant space.
This solution is essentially a rule-based classification problem from math and simulation. The main goal is translating constraints into precise boolean logic without missing edge cases.
Approach 2: Short-Circuit Evaluation and Combined Checks (O(1) time, O(1) space)
This variation keeps the same logic but reduces branching by combining conditions using boolean expressions. Instead of multiple nested checks, compute isBulky and isHeavy in a single expression each. Short-circuit evaluation ensures unnecessary checks are skipped once a condition becomes true. For example, the bulky condition can be evaluated as (length ≥ 10000 || width ≥ 10000 || height ≥ 10000 || volume ≥ 1e9).
After evaluating the two flags, return the correct label using compact logic such as checking both flags first, then each individual flag. The complexity remains constant, but the code becomes shorter and easier to scan during interviews. This pattern is common in problems involving rule evaluation and logical grouping.
Recommended for interviews: The simple conditional checks approach is exactly what interviewers expect. It shows you correctly interpret constraints and translate them into clean boolean logic. The short-circuit variant is slightly cleaner and demonstrates comfort with concise condition evaluation, but both run in O(1) time and O(1) space.
This approach involves straightforward conditional checks to determine whether a box is "Bulky" or "Heavy". For the "Bulky" condition, check if any dimension is >= 10,000 or if the volume (length * width * height) is >= 1,000,000,000. Then, check if the mass is >= 100 for the "Heavy" condition. Based on these conditions, deduce the category of the box.
This C solution checks whether a box is "Bulky" based on its dimensions and volume, and whether it is "Heavy" based on its mass. It then uses if-else statements to determine the category of the box based on these flags. This approach makes use of a long data type to handle potential overflow in volume calculation.
Time Complexity: O(1) because we only perform basic arithmetic operations and comparisons.
Space Complexity: O(1) since no extra space is used apart from a few variables.
This approach uses short-circuit evaluation to efficiently determine if the box is "Bulky" or "Heavy" by combining checks into a single return statement. The approach leverages logical operators to decide the category in a more concise manner.
This solution in C evaluates whether the box is "Bulky" or "Heavy" through single-line conditional checks using the ternary operator. Logical and relational operations help form a single statement for determining the outcome.
Time Complexity: O(1).
Space Complexity: O(1).
We can simulate according to the problem description.
The time complexity is O(1), and the space complexity is O(1).
| Approach | Complexity |
|---|---|
| Simple Conditional Checks | Time Complexity: O(1) because we only perform basic arithmetic operations and comparisons. |
| Short-Circuit Evaluation and Combined Checks | Time Complexity: O(1). |
| Simulation | — |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simple Conditional Checks | O(1) | O(1) | Best for clarity and interviews where translating problem rules into conditions is expected |
| Short-Circuit Evaluation and Combined Checks | O(1) | O(1) | Useful when writing compact logic with fewer branches and cleaner boolean expressions |
6287. Categorize Box According to Criteria || Leetcode || Biweekly Contest 95||java || • Vishwas aithal • 367 views views
Watch 9 more video solutions →Practice Categorize Box According to Criteria with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor