[LeetCode] 2525. Categorize Box According to Criteria

发布时间 2023-10-20 11:03:28作者: CNoodle

Given four integers lengthwidthheight, 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:

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

根据规则将箱子分类。

给你四个整数 length ,width ,height 和 mass ,分别表示一个箱子的三个维度和质量,请你返回一个表示箱子 类别 的字符串。

  • 如果满足以下条件,那么箱子是 "Bulky" 的:
    • 箱子 至少有一个 维度大于等于 104 。
    • 或者箱子的 体积 大于等于 109 。
  • 如果箱子的质量大于等于 100 ,那么箱子是 "Heavy" 的。
  • 如果箱子同时是 "Bulky" 和 "Heavy" ,那么返回类别为 "Both" 。
  • 如果箱子既不是 "Bulky" ,也不是 "Heavy" ,那么返回类别为 "Neither" 。
  • 如果箱子是 "Bulky" 但不是 "Heavy" ,那么返回类别为 "Bulky" 。
  • 如果箱子是 "Heavy" 但不是 "Bulky" ,那么返回类别为 "Heavy" 。

注意,箱子的体积等于箱子的长度、宽度和高度的乘积。

思路是根据题意判断即可。注意在判断体积的时候需要用 long 型否则会有个别 testcase 过不去。

时间O(1)

空间O(1)

Java实现

 1 class Solution {
 2     public String categorizeBox(int length, int width, int height, int mass) {
 3         boolean bulky = false;
 4         boolean heavy = false;
 5         // bulky?
 6         if (length >= 10000 || width >= 10000 || height >= 10000) {
 7             bulky = true;
 8         }
 9 
10         // volume
11         if ((long) length * width * height >= (long) Math.pow(10, 9)) {
12             bulky = true;
13         }
14 
15         // heavy?
16         if (mass >= 100) {
17             heavy = true;
18         }
19 
20         // both
21         if (bulky == true && heavy == true) {
22             return "Both";
23         }
24         // Bulky
25         else if (bulky == true) {
26             return "Bulky";
27         }
28         else if (heavy == true) {
29             return "Heavy";
30         }
31         return "Neither";
32     }
33 }

 

LeetCode 题目总结