LeetCode 题解工作台
根据规则将箱子分类
给你四个整数 length , width , height 和 mass ,分别表示一个箱子的三个维度和质量,请你返回一个表示箱子 类别 的字符串。 如果满足以下条件,那么箱子是 "Bulky" 的: 箱子 至少有一个 维度大于等于 10 4 。 或者箱子的 体积 大于等于 10 9 。 如果箱子…
1
题型
6
代码语言
3
相关题
当前训练重点
简单 · 数学·driven
答案摘要
根据题意模拟即可。 时间复杂度 ,空间复杂度 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数学·driven 题型思路
题目描述
给你四个整数 length ,width ,height 和 mass ,分别表示一个箱子的三个维度和质量,请你返回一个表示箱子 类别 的字符串。
- 如果满足以下条件,那么箱子是
"Bulky"的:- 箱子 至少有一个 维度大于等于
104。 - 或者箱子的 体积 大于等于
109。
- 箱子 至少有一个 维度大于等于
- 如果箱子的质量大于等于
100,那么箱子是"Heavy"的。 - 如果箱子同时是
"Bulky"和"Heavy",那么返回类别为"Both"。 - 如果箱子既不是
"Bulky",也不是"Heavy",那么返回类别为"Neither"。 - 如果箱子是
"Bulky"但不是"Heavy",那么返回类别为"Bulky"。 - 如果箱子是
"Heavy"但不是"Bulky",那么返回类别为"Heavy"。
注意,箱子的体积等于箱子的长度、宽度和高度的乘积。
示例 1:
输入:length = 1000, width = 35, height = 700, mass = 300 输出:"Heavy" 解释: 箱子没有任何维度大于等于 104 。 体积为 24500000 <= 109 。所以不能归类为 "Bulky" 。 但是质量 >= 100 ,所以箱子是 "Heavy" 的。 由于箱子不是 "Bulky" 但是是 "Heavy" ,所以我们返回 "Heavy" 。
示例 2:
输入:length = 200, width = 50, height = 800, mass = 50 输出:"Neither" 解释: 箱子没有任何维度大于等于 104 。 体积为 8 * 106 <= 109 。所以不能归类为 "Bulky" 。 质量小于 100 ,所以不能归类为 "Heavy" 。 由于不属于上述两者任何一类,所以我们返回 "Neither" 。
提示:
1 <= length, width, height <= 1051 <= mass <= 103
解题思路
方法一:模拟
根据题意模拟即可。
时间复杂度 ,空间复杂度 。
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]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Tests ability to apply conditional statements in programming.
- question_mark
Assesses understanding of basic mathematical operations and logic.
- question_mark
Evaluates ability to use simple problem constraints effectively.
常见陷阱
外企场景- error
Forgetting to calculate the volume before checking the categories.
- error
Incorrectly categorizing a box due to misplaced or missing condition checks.
- error
Misinterpreting the thresholds for volume and mass, leading to an incorrect category.
进阶变体
外企场景- arrow_right_alt
Consider variations where additional dimensions or categories are introduced.
- arrow_right_alt
Implementing the problem in a different context, such as 3D objects or irregular shapes.
- arrow_right_alt
Enhancing the problem by adding constraints such as specific ranges for length, width, height, and mass.