2525. Categorize Box According to Criteria LeetCode Solution

In this guide, you will get 2525. Categorize Box According to Criteria LeetCode Solution with the best time and space complexity. The solution to Categorize Box According to Criteria problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Categorize Box According to Criteria solution in C++
  4. Categorize Box According to Criteria solution in Java
  5. Categorize Box According to Criteria solution in Python
  6. Additional Resources
2525. Categorize Box According to Criteria LeetCode Solution image

Problem Statement of Categorize Box According to Criteria

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:

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”.

Note that the volume of the box is the product of its length, width and height.

Example 1:

See also  142. Linked List Cycle II LeetCode Solution

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 = 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

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

2525. Categorize Box According to Criteria LeetCode Solution in C++

class Solution {
 public:
  string categorizeBox(int length, int width, int height, int mass) {
    const bool isBulky =
        length >= 10000 || width >= 10000 || height >= 10000 ||
        static_cast<long>(length) * width * height >= 1'000'000'000;
    const bool isHeavy = mass >= 100;
    if (isBulky && isHeavy)
      return "Both";
    if (isBulky)
      return "Bulky";
    if (isHeavy)
      return "Heavy";
    return "Neither";
  }
};
/* code provided by PROGIEZ */

2525. Categorize Box According to Criteria LeetCode Solution in Java

class Solution {
  public String categorizeBox(int length, int width, int height, int mass) {
    final boolean isBulky = length >= 10000 || width >= 10000 || height >= 10000 ||
                            (long) length * width * height >= 1_000_000_000;
    final boolean isHeavy = mass >= 100;
    if (isBulky && isHeavy)
      return "Both";
    if (isBulky)
      return "Bulky";
    if (isHeavy)
      return "Heavy";
    return "Neither";
  }
}
// code provided by PROGIEZ

2525. Categorize Box According to Criteria LeetCode Solution in Python

class Solution:
  def categorizeBox(
      self,
      length: int,
      width: int,
      height: int,
      mass: int,
  ) -> str:
    isBulky = (length >= 10000 or
               width >= 10000 or height >= 10000 or
               length * width * height >= 1_000_000_000)
    isHeavy = mass >= 100
    if isBulky and isHeavy:
      return 'Both'
    if isBulky:
      return 'Bulky'
    if isHeavy:
      return 'Heavy'
    return 'Neither'
# code by PROGIEZ

Additional Resources

See also  1873. Calculate Special Bonus LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.