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"
.
- If the mass of the box is greater or equal to
Note that the volume of the box is the product of its length, width and height.
Examples
Example 1
|
|
Example 2
|
|
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:
- Calculate the volume as length × width × height.
- Check if any dimension ≥ 10^4 or volume ≥ 10^9 to determine if the box is “Bulky”.
- Check if mass ≥ 100 to determine if the box is “Heavy”.
- 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
|
|
|
|
Complexity
- ⏰ Time complexity:
O(1)
- 🧺 Space complexity:
O(1)