返回介绍

solution / 2500-2599 / 2525.Categorize Box According to Criteria / README_EN

发布于 2024-06-17 01:03:04 字数 8850 浏览 0 评论 0 收藏 0

2525. Categorize Box According to Criteria

中文文档

Description

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:

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

Solutions

Solution 1: Simulation

We can simulate according to the problem description.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

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]
class Solution {
  public String categorizeBox(int length, int width, int height, int mass) {
    long v = (long) length * width * height;
    int bulky = length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 ? 1 : 0;
    int heavy = mass >= 100 ? 1 : 0;
    String[] d = {"Neither", "Bulky", "Heavy", "Both"};
    int i = heavy << 1 | bulky;
    return d[i];
  }
}
class Solution {
public:
  string categorizeBox(int length, int width, int height, int mass) {
    long v = (long) length * width * height;
    int bulky = length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 ? 1 : 0;
    int heavy = mass >= 100 ? 1 : 0;
    string d[4] = {"Neither", "Bulky", "Heavy", "Both"};
    int i = heavy << 1 | bulky;
    return d[i];
  }
};
func categorizeBox(length int, width int, height int, mass int) string {
  v := length * width * height
  i := 0
  if length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 {
    i |= 1
  }
  if mass >= 100 {
    i |= 2
  }
  d := [4]string{"Neither", "Bulky", "Heavy", "Both"}
  return d[i]
}
function categorizeBox(length: number, width: number, height: number, mass: number): string {
  const v = length * width * height;
  let i = 0;
  if (length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000) {
    i |= 1;
  }
  if (mass >= 100) {
    i |= 2;
  }
  return ['Neither', 'Bulky', 'Heavy', 'Both'][i];
}
impl Solution {
  pub fn categorize_box(length: i32, width: i32, height: i32, mass: i32) -> String {
    let v = (length as i64) * (width as i64) * (height as i64);
    let mut i = 0;

    if length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 {
      i |= 1;
    }

    if mass >= 100 {
      i |= 2;
    }

    let d = vec!["Neither", "Bulky", "Heavy", "Both"];
    d[i].to_string()
  }
}

Solution 2

class Solution:
  def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
    v = length * width * height
    bulky = any(x >= 10000 for x in (length, width, height)) or v >= 10**9
    heavy = mass >= 100

    if bulky and heavy:
      return "Both"
    if bulky:
      return "Bulky"
    if heavy:
      return "Heavy"

    return "Neither"
class Solution {
  public String categorizeBox(int length, int width, int height, int mass) {
    long v = (long) length * width * height;
    boolean bulky = length >= 1e4 || width >= 1e4 || height >= 1e4 || v >= 1e9;
    boolean heavy = mass >= 100;

    if (bulky && heavy) {
      return "Both";
    }
    if (bulky) {
      return "Bulky";
    }
    if (heavy) {
      return "Heavy";
    }

    return "Neither";
  }
}
class Solution {
public:
  string categorizeBox(int length, int width, int height, int mass) {
    long v = (long) length * width * height;
    bool bulky = length >= 1e4 || width >= 1e4 || height >= 1e4 || v >= 1e9;
    bool heavy = mass >= 100;

    if (bulky && heavy) {
      return "Both";
    }
    if (bulky) {
      return "Bulky";
    }
    if (heavy) {
      return "Heavy";
    }

    return "Neither";
  }
};
func categorizeBox(length int, width int, height int, mass int) string {
  v := length * width * height
  bulky := length >= 1e4 || width >= 1e4 || height >= 1e4 || v >= 1e9
  heavy := mass >= 100
  if bulky && heavy {
    return "Both"
  }
  if bulky {
    return "Bulky"
  }
  if heavy {
    return "Heavy"
  }
  return "Neither"
}
function categorizeBox(length: number, width: number, height: number, mass: number): string {
  const v = length * width * height;
  const bulky = length >= 1e4 || width >= 1e4 || height >= 1e4 || v >= 1e9;
  const heavy = mass >= 100;
  if (bulky && heavy) {
    return 'Both';
  }
  if (bulky) {
    return 'Bulky';
  }
  if (heavy) {
    return 'Heavy';
  }
  return 'Neither';
}
impl Solution {
  pub fn categorize_box(length: i32, width: i32, height: i32, mass: i32) -> String {
    let v = length * width * height;
    let bulky =
      length >= 10000 ||
      width >= 10000 ||
      height >= 10000 ||
      (length as i64) * (width as i64) * (height as i64) >= 1000000000;

    let heavy = mass >= 100;

    if bulky && heavy {
      return "Both".to_string();
    }
    if bulky {
      return "Bulky".to_string();
    }
    if heavy {
      return "Heavy".to_string();
    }

    "Neither".to_string()
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文