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 <= 103This 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.
C++
Java
Python
C#
JavaScript
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.
C++
Java
Python
C#
JavaScript
Time Complexity: O(1).
Space Complexity: 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). |
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