algoadvance

Leetcode 2525. Categorize Box According to Criteria

Problem Statement:

You are given the dimensions of a box (length, width, height) and the weight of the box. The box needs to be categorized based on the following criteria:

  1. Bulky: A box is bulky if any of its dimensions is greater than or equal to 10^4 units or its volume is greater than or equal to 10^9 cubic units.
  2. Heavy: A box is heavy if its weight is greater than or equal to 100 units.
  3. Neither: A box that is neither bulky nor heavy should be categorized as “Neither”.
  4. Both: A box that is both bulky and heavy should be categorized as “Both”.

Your task is to write a function that takes the dimensions and weight of the box and returns the appropriate category.

Clarifying Questions:

  1. Input Range: What are the ranges for the dimensions and weight of the box?
    • Assumption: The dimensions and weight of the box are positive integers within a reasonable range (e.g., within a 32-bit signed integer).
  2. Volume Calculation: Do we need to calculate the volume using all three dimensions?
    • Yes, the volume is calculated as length * width * height.

Strategy:

  1. Bulky Criteria: Check if any of the dimensions is >= 10^4 or if the volume of the box >= 10^9.
  2. Heavy Criteria: Check if the weight >= 100.
  3. Both Criteria: If the box meets both the bulky and heavy criteria, it’s categorized as “Both”.
  4. Neither Criteria: If the box doesn’t meet either bulky or heavy criteria, it’s categorized as “Neither”.

Code:

public class BoxCategorizer {
    public String categorizeBox(int length, int width, int height, int weight) {
        boolean isBulky = length >= 10000 || width >= 10000 || height >= 10000 || ((long)length * width * height >= 1000000000);
        boolean isHeavy = weight >= 100;
        
        if (isBulky && isHeavy) {
            return "Both";
        } else if (isBulky) {
            return "Bulky";
        } else if (isHeavy) {
            return "Heavy";
        } else {
            return "Neither";
        }
    }
    
    public static void main(String[] args) {
        BoxCategorizer bc = new BoxCategorizer();
        System.out.println(bc.categorizeBox(10000, 5000, 2000, 30)); // Output: Bulky
        System.out.println(bc.categorizeBox(5000, 5000, 1000, 120)); // Output: Heavy
        System.out.println(bc.categorizeBox(10000, 5000, 2000, 120)); // Output: Both
        System.out.println(bc.categorizeBox(5, 5, 5, 5)); // Output: Neither
    }
}

Time Complexity:

This simple and efficient approach fulfills the requirements of categorizing the box based on the given criteria.

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI