Skip to content

Commit 798f820

Browse files
committed
添加了问题“2525.根据规则将箱子分类”的代码和题解
1 parent c4ced24 commit 798f820

File tree

5 files changed

+144
-2
lines changed

5 files changed

+144
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2023-10-20 12:03:20
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2023-10-20 12:08:57
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
const string dict[4] = {"Neither", "Heavy", "Bulky", "Both"};
12+
13+
typedef long long ll;
14+
class Solution {
15+
public:
16+
string categorizeBox(ll length, ll width, ll height, ll mass) {
17+
bool bulky = length >= 10000 || width >= 10000 || height >= 10000 || length * width * height >= 1000000000;
18+
bool heavy = mass >= 100;
19+
return dict[bulky * 2 + heavy];
20+
}
21+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2023-10-20 12:09:52
4+
LastEditors: LetMeFly
5+
LastEditTime: 2023-10-20 12:11:46
6+
'''
7+
dic = ['Neither', 'Heavy', 'Bulky', 'Both']
8+
class Solution:
9+
def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
10+
bulky = length >= 10000 or width >= 10000 or height >= 10000 or length * width * height >= 1000000000
11+
heavy = mass >= 100
12+
return dic[bulky * 2 + heavy]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ int main() {
509509
|2500.删除每行中的最大值|简单|<a href="https://leetcode.cn/problems/delete-greatest-value-in-each-row/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/07/27/LeetCode%202500.%E5%88%A0%E9%99%A4%E6%AF%8F%E8%A1%8C%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131951838" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/delete-greatest-value-in-each-row/solutions/2360649/letmefly-2500shan-chu-mei-xing-zhong-de-8ks5c/" target="_blank">LeetCode题解</a>|
510510
|2511.最多可以摧毁的敌人城堡数目|简单|<a href="https://leetcode.cn/problems/maximum-enemy-forts-that-can-be-captured/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/09/02/LeetCode%202511.%E6%9C%80%E5%A4%9A%E5%8F%AF%E4%BB%A5%E6%91%A7%E6%AF%81%E7%9A%84%E6%95%8C%E4%BA%BA%E5%9F%8E%E5%A0%A1%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132634912" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-enemy-forts-that-can-be-captured/solutions/2422392/letmefly-2511zui-duo-ke-yi-cui-hui-de-di-r7kt/" target="_blank">LeetCode题解</a>|
511511
|2512.奖励最顶尖的K名学生|中等|<a href="https://leetcode.cn/problems/reward-top-k-students/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/11/LeetCode%202512.%E5%A5%96%E5%8A%B1%E6%9C%80%E9%A1%B6%E5%B0%96%E7%9A%84K%E5%90%8D%E5%AD%A6%E7%94%9F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133762019" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reward-top-k-students/solutions/2477204/letmefly-2512jiang-li-zui-ding-jian-de-k-o0fg/" target="_blank">LeetCode题解</a>|
512+
|2525.根据规则将箱子分类|简单|<a href="https://leetcode.cn/problems/categorize-box-according-to-criteria/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/20/LeetCode%202525.%E6%A0%B9%E6%8D%AE%E8%A7%84%E5%88%99%E5%B0%86%E7%AE%B1%E5%AD%90%E5%88%86%E7%B1%BB/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133943695" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/categorize-box-according-to-criteria/solutions/2490699/letmefly-2525gen-ju-gui-ze-jiang-xiang-z-h2gu/" target="_blank">LeetCode题解</a>|
512513
|2530.执行K次操作后的最大分数|中等|<a href="https://leetcode.cn/problems/maximal-score-after-applying-k-operations/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/18/LeetCode%202530.%E6%89%A7%E8%A1%8CK%E6%AC%A1%E6%93%8D%E4%BD%9C%E5%90%8E%E7%9A%84%E6%9C%80%E5%A4%A7%E5%88%86%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133899145" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximal-score-after-applying-k-operations/solutions/2487511/letmefly-2530zhi-xing-k-ci-cao-zuo-hou-d-s4pm/" target="_blank">LeetCode题解</a>|
513514
|2544.交替数字和|简单|<a href="https://leetcode.cn/problems/alternating-digit-sum/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/07/12/LeetCode%202544.%E4%BA%A4%E6%9B%BF%E6%95%B0%E5%AD%97%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131673485" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/alternating-digit-sum/solutions/2340450/letmefly-2544jiao-ti-shu-zi-he-by-tisfy-aa0f/" target="_blank">LeetCode题解</a>|
514515
|2559.统计范围内的元音字符串数|中等|<a href="https://leetcode.cn/problems/count-vowel-strings-in-ranges/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/06/02/LeetCode%202559.%E7%BB%9F%E8%AE%A1%E8%8C%83%E5%9B%B4%E5%86%85%E7%9A%84%E5%85%83%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131014779" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-vowel-strings-in-ranges/solutions/2294361/letmefly-2559tong-ji-fan-wei-nei-de-yuan-mq4f/" target="_blank">LeetCode题解</a>|
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: 2525.根据规则将箱子分类
3+
date: 2023-10-20 12:16:14
4+
tags: [题解, LeetCode, 简单, 数学]
5+
---
6+
7+
# 【LetMeFly】2525.根据规则将箱子分类:优雅解法?
8+
9+
力扣题目链接:[https://leetcode.cn/problems/categorize-box-according-to-criteria/](https://leetcode.cn/problems/categorize-box-according-to-criteria/)
10+
11+
<p>给你四个整数&nbsp;<code>length</code>&nbsp;,<code>width</code>&nbsp;,<code>height</code>&nbsp;&nbsp;<code>mass</code>&nbsp;,分别表示一个箱子的三个维度和质量,请你返回一个表示箱子 <strong>类别</strong> 的字符串。</p>
12+
13+
<ul>
14+
<li>如果满足以下条件,那么箱子是&nbsp;<code>"Bulky"</code>&nbsp;的:
15+
16+
<ul>
17+
<li>箱子 <strong>至少有一个</strong> 维度大于等于 <code>10<sup>4</sup></code>&nbsp;。</li>
18+
<li>或者箱子的 <strong>体积</strong> 大于等于&nbsp;<code>10<sup>9</sup></code>&nbsp;。</li>
19+
</ul>
20+
</li>
21+
<li>如果箱子的质量大于等于&nbsp;<code>100</code>&nbsp;,那么箱子是&nbsp;<code>"Heavy"</code>&nbsp;的。</li>
22+
<li>如果箱子同时是&nbsp;<code>"Bulky"</code> 和&nbsp;<code>"Heavy"</code>&nbsp;,那么返回类别为&nbsp;<code>"Both"</code>&nbsp;。</li>
23+
<li>如果箱子既不是&nbsp;<code>"Bulky"</code>&nbsp;,也不是&nbsp;<code>"Heavy"</code>&nbsp;,那么返回类别为&nbsp;<code>"Neither"</code>&nbsp;。</li>
24+
<li>如果箱子是&nbsp;<code>"Bulky"</code>&nbsp;但不是&nbsp;<code>"Heavy"</code>&nbsp;,那么返回类别为&nbsp;<code>"Bulky"</code>&nbsp;。</li>
25+
<li>如果箱子是&nbsp;<code>"Heavy"</code>&nbsp;但不是&nbsp;<code>"Bulky"</code>&nbsp;,那么返回类别为&nbsp;<code>"Heavy"</code>&nbsp;。</li>
26+
</ul>
27+
28+
<p><strong>注意</strong>,箱子的体积等于箱子的长度、宽度和高度的乘积。</p>
29+
30+
<p>&nbsp;</p>
31+
32+
<p><strong>示例 1:</strong></p>
33+
34+
<pre>
35+
<b>输入:</b>length = 1000, width = 35, height = 700, mass = 300
36+
<b>输出:</b>"Heavy"
37+
<b>解释:</b>
38+
箱子没有任何维度大于等于 10<sup>4 </sup>。
39+
体积为 24500000 &lt;= 10<sup>9 </sup>。所以不能归类为 "Bulky" 。
40+
但是质量 &gt;= 100 ,所以箱子是 "Heavy" 的。
41+
由于箱子不是 "Bulky" 但是是 "Heavy" ,所以我们返回 "Heavy" 。</pre>
42+
43+
<p><strong>示例 2:</strong></p>
44+
45+
<pre>
46+
<b>输入:</b>length = 200, width = 50, height = 800, mass = 50
47+
<b>输出:</b>"Neither"
48+
<b>解释:</b>
49+
箱子没有任何维度大于等于 10<sup>4</sup>&nbsp;
50+
体积为 8 * 10<sup>6</sup> &lt;= 10<sup>9</sup>&nbsp;。所以不能归类为 "Bulky" 。
51+
质量小于 100 ,所以不能归类为 "Heavy" 。
52+
由于不属于上述两者任何一类,所以我们返回 "Neither" 。</pre>
53+
54+
<p>&nbsp;</p>
55+
56+
<p><strong>提示:</strong></p>
57+
58+
<ul>
59+
<li><code>1 &lt;= length, width, height &lt;= 10<sup>5</sup></code></li>
60+
<li><code>1 &lt;= mass &lt;= 10<sup>3</sup></code></li>
61+
</ul>
62+
63+
64+
65+
## 方法一:优雅解法?
66+
67+
判断箱子是否符合```bulky``````heavy```很简单,对于一些编程语言注意不要“32位整数溢出”就可以了。
68+
69+
得到了值为```0``````1```的两个变量```bulky``````heavy```,怎么“优雅”地转为字符串返回呢?
70+
71+
可以预先定义一个字符串数组```dic = ['Neither', 'Heavy', 'Bulky', 'Both']```,这样直接返回```dic[bulky * 2 + heavy]```就可以了。本质上是将这两个变量看成了```dic```下标二进制下的低两位,这样就避免了四个```if-else```的出现。
72+
73+
+ 时间复杂度$O(1)$
74+
+ 空间复杂度$O(1)$
75+
76+
### AC代码
77+
78+
#### C++
79+
80+
```cpp
81+
const string dict[4] = {"Neither", "Heavy", "Bulky", "Both"};
82+
83+
typedef long long ll;
84+
class Solution {
85+
public:
86+
string categorizeBox(ll length, ll width, ll height, ll mass) {
87+
bool bulky = length >= 10000 || width >= 10000 || height >= 10000 || length * width * height >= 1000000000;
88+
bool heavy = mass >= 100;
89+
return dict[bulky * 2 + heavy];
90+
}
91+
};
92+
```
93+
94+
#### Python
95+
96+
```python
97+
dic = ['Neither', 'Heavy', 'Bulky', 'Both']
98+
class Solution:
99+
def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
100+
bulky = length >= 10000 or width >= 10000 or height >= 10000 or length * width * height >= 1000000000
101+
heavy = mass >= 100
102+
return dic[bulky * 2 + heavy]
103+
104+
```
105+
106+
> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2023/10/20/LeetCode%202525.%E6%A0%B9%E6%8D%AE%E8%A7%84%E5%88%99%E5%B0%86%E7%AE%B1%E5%AD%90%E5%88%86%E7%B1%BB/)~
107+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/133943695](https://letmefly.blog.csdn.net/article/details/133943695)

newSolution.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: LetMeFly
33
Date: 2022-07-03 11:21:14
44
LastEditors: LetMeFly
5-
LastEditTime: 2023-10-20 11:59:02
5+
LastEditTime: 2023-10-20 12:15:54
66
Command: python newSolution.py 102. 二叉树的层序遍历
77
What's more: 当前仅支持数字开头的题目
88
'''
@@ -74,7 +74,8 @@ def refreshPublistTime(solution: str) -> solution:
7474

7575
# 认领issue
7676
os.system(f'git checkout -b {num}')
77-
issueCreateResult = os.popen(f'gh issue create -t "Add 1 more problem of {num}"').read()
77+
issueCreateResult = os.popen(f'gh issue create -t "Add 1 more problem of {num}" -b "By newSolution.py using GH"').read()
78+
print(issueCreateResult)
7879
issueNum = int(issueCreateResult.split('\n')[0].split('/')[-1])
7980

8081
print("请编辑题解: “{0}”,注意不要更改文件前5行".format(solutionName))

0 commit comments

Comments
 (0)