LeetCode 2525. Categorize Box According to Criteria Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
2525. Categorize Box According to Criteria

Description

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.

  • The box is "Bulky" if:
    <ul>
    	<li><strong>Any</strong> of the dimensions of the box is greater or equal to <code>10<sup>4</sup></code>.</li>
    	<li>Or, the <strong>volume</strong> of the box is greater or equal to <code>10<sup>9</sup></code>.</li>
    </ul>
    </li>
    <li>If the mass of the box is greater or equal to <code>100</code>, it is <code>&quot;Heavy&quot;.</code></li>
    <li>If the box is both <code>&quot;Bulky&quot;</code> and <code>&quot;Heavy&quot;</code>, then its category is <code>&quot;Both&quot;</code>.</li>
    <li>If the box is neither <code>&quot;Bulky&quot;</code> nor <code>&quot;Heavy&quot;</code>, then its category is <code>&quot;Neither&quot;</code>.</li>
    <li>If the box is <code>&quot;Bulky&quot;</code> but not <code>&quot;Heavy&quot;</code>, then its category is <code>&quot;Bulky&quot;</code>.</li>
    <li>If the box is <code>&quot;Heavy&quot;</code> but not <code>&quot;Bulky&quot;</code>, then its category is <code>&quot;Heavy&quot;</code>.</li>
    

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 <= 105
  • 1 <= mass <= 103

Solutions

Solution 1: Simulation

We can simulate according to the problem description.

The time complexity is O(1), and the space complexity is O(1).

PythonJavaC++GoTypeScriptRust
class Solution: def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str: v = length * width * height bulky = int(any(x >= 10000 for x in (length, width, height)) or v >= 10**9) heavy = int(mass >= 100) i = heavy << 1 | bulky d = ['Neither', 'Bulky', 'Heavy', 'Both'] return d[i](code-box)

Solution 2

PythonJavaC++GoTypeScriptRust
class Solution: def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str: v = length * width * height bulky = any(x >= 10000 for x in (length, width, height)) or v >= 10**9 heavy = mass >= 100 if bulky and heavy: return "Both" if bulky: return "Bulky" if heavy: return "Heavy" return "Neither"(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !