LeetCode 2525: Categorize Box According to Criteria (Bulky/Heavy Rule Matrix)
LeetCode 2525SimulationConditional LogicToday we solve LeetCode 2525 - Categorize Box According to Criteria.
Source: https://leetcode.com/problems/categorize-box-according-to-criteria/
English
Problem Summary
Given a box with dimensions length, width, height, and mass, classify it:
- Bulky if any dimension is at least 10^4, or volume is at least 10^9.
- Heavy if mass is at least 100.
Return:
- Both if bulky and heavy.
- Bulky if only bulky.
- Heavy if only heavy.
- Neither otherwise.
Key Insight
This is a direct boolean classification problem. Compute two flags:
isBulky and isHeavy, then map the 4 possible flag combinations to the final string.
Algorithm
- Compute isBulky = (maxDimension >= 10000) || (length * width * height >= 1e9).
- Compute isHeavy = (mass >= 100).
- Return based on combination of isBulky and isHeavy.
Complexity Analysis
Time: O(1).
Space: O(1).
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public String categorizeBox(int length, int width, int height, int mass) {
long volume = 1L * length * width * height;
boolean isBulky = length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1_000_000_000L;
boolean isHeavy = mass >= 100;
if (isBulky && isHeavy) return "Both";
if (isBulky) return "Bulky";
if (isHeavy) return "Heavy";
return "Neither";
}
}func categorizeBox(length int, width int, height int, mass int) string {
volume := int64(length) * int64(width) * int64(height)
isBulky := length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1_000_000_000
isHeavy := mass >= 100
if isBulky && isHeavy {
return "Both"
}
if isBulky {
return "Bulky"
}
if isHeavy {
return "Heavy"
}
return "Neither"
}class Solution {
public:
string categorizeBox(int length, int width, int height, int mass) {
long long volume = 1LL * length * width * height;
bool isBulky = length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000LL;
bool isHeavy = mass >= 100;
if (isBulky && isHeavy) return "Both";
if (isBulky) return "Bulky";
if (isHeavy) return "Heavy";
return "Neither";
}
};class Solution:
def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
volume = length * width * height
is_bulky = length >= 10_000 or width >= 10_000 or height >= 10_000 or volume >= 1_000_000_000
is_heavy = mass >= 100
if is_bulky and is_heavy:
return "Both"
if is_bulky:
return "Bulky"
if is_heavy:
return "Heavy"
return "Neither"var categorizeBox = function(length, width, height, mass) {
const volume = BigInt(length) * BigInt(width) * BigInt(height);
const isBulky =
length >= 10000 ||
width >= 10000 ||
height >= 10000 ||
volume >= 1000000000n;
const isHeavy = mass >= 100;
if (isBulky && isHeavy) return "Both";
if (isBulky) return "Bulky";
if (isHeavy) return "Heavy";
return "Neither";
};中文
题目概述
给定箱子的 length、width、height 和 mass,进行分类:
- 若任一维度 >= 10^4,或体积 >= 10^9,则为 Bulky。
- 若质量 mass >= 100,则为 Heavy。
最终返回:
- 同时满足返回 Both;
- 仅 bulky 返回 Bulky;
- 仅 heavy 返回 Heavy;
- 都不满足返回 Neither。
核心思路
本题本质是布尔条件组合:
先算出 isBulky 和 isHeavy 两个标记,再根据四种组合返回对应字符串。
算法步骤
- 计算 isBulky = (任一维度 >= 10000) 或 (体积 >= 1e9)。
- 计算 isHeavy = (mass >= 100)。
- 按两个布尔值组合映射到最终结果。
复杂度分析
时间复杂度:O(1)。
空间复杂度:O(1)。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public String categorizeBox(int length, int width, int height, int mass) {
long volume = 1L * length * width * height;
boolean isBulky = length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1_000_000_000L;
boolean isHeavy = mass >= 100;
if (isBulky && isHeavy) return "Both";
if (isBulky) return "Bulky";
if (isHeavy) return "Heavy";
return "Neither";
}
}func categorizeBox(length int, width int, height int, mass int) string {
volume := int64(length) * int64(width) * int64(height)
isBulky := length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1_000_000_000
isHeavy := mass >= 100
if isBulky && isHeavy {
return "Both"
}
if isBulky {
return "Bulky"
}
if isHeavy {
return "Heavy"
}
return "Neither"
}class Solution {
public:
string categorizeBox(int length, int width, int height, int mass) {
long long volume = 1LL * length * width * height;
bool isBulky = length >= 10000 || width >= 10000 || height >= 10000 || volume >= 1000000000LL;
bool isHeavy = mass >= 100;
if (isBulky && isHeavy) return "Both";
if (isBulky) return "Bulky";
if (isHeavy) return "Heavy";
return "Neither";
}
};class Solution:
def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
volume = length * width * height
is_bulky = length >= 10_000 or width >= 10_000 or height >= 10_000 or volume >= 1_000_000_000
is_heavy = mass >= 100
if is_bulky and is_heavy:
return "Both"
if is_bulky:
return "Bulky"
if is_heavy:
return "Heavy"
return "Neither"var categorizeBox = function(length, width, height, mass) {
const volume = BigInt(length) * BigInt(width) * BigInt(height);
const isBulky =
length >= 10000 ||
width >= 10000 ||
height >= 10000 ||
volume >= 1000000000n;
const isHeavy = mass >= 100;
if (isBulky && isHeavy) return "Both";
if (isBulky) return "Bulky";
if (isHeavy) return "Heavy";
return "Neither";
};
Comments