Skip to content

Commit b9eb7de

Browse files
committed
feat: add new lc problems
1 parent 957a49a commit b9eb7de

File tree

51 files changed

+3283
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+3283
-20
lines changed

solution/2100-2199/2185.Counting Words With a Given Prefix/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45-
**方法一:枚举**
45+
**方法一:一次遍历**
4646

47-
我们可以枚举字符串数组 `words` 中的每个字符串 $w$,判断其是否以 $pref$ 作为前缀,如果是,则答案加一。
47+
根据题目描述,我们遍历字符串数组 `words` 中的每个字符串 $w$,判断其是否以 $pref$ 作为前缀,如果是,则答案加一。
4848

4949
时间复杂度 $O(n \times m)$,空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别是字符串数组 `words` 和字符串 $pref$ 的长度。
5050

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# [2525. 根据规则将箱子分类](https://leetcode.cn/problems/categorize-box-according-to-criteria)
2+
3+
[English Version](/solution/2500-2599/2525.Categorize%20Box%20According%20to%20Criteria/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你四个整数&nbsp;<code>length</code>&nbsp;,<code>width</code>&nbsp;,<code>height</code>&nbsp;&nbsp;<code>mass</code>&nbsp;,分别表示一个箱子的三个维度和质量,请你返回一个表示箱子 <strong>类别</strong> 的字符串。</p>
10+
11+
<ul>
12+
<li>如果满足以下条件,那么箱子是&nbsp;<code>"Bulky"</code>&nbsp;的:
13+
14+
<ul>
15+
<li>箱子 <strong>至少有一个</strong> 维度大于等于 <code>10<sup>4</sup></code>&nbsp;。</li>
16+
<li>或者箱子的 <strong>体积</strong> 大于等于&nbsp;<code>10<sup>9</sup></code>&nbsp;。</li>
17+
</ul>
18+
</li>
19+
<li>如果箱子的质量大于等于&nbsp;<code>100</code>&nbsp;,那么箱子是&nbsp;<code>"Heavy"</code>&nbsp;的。</li>
20+
<li>如果箱子同时是&nbsp;<code>"Bulky"</code> 和&nbsp;<code>"Heavy"</code>&nbsp;,那么返回类别为&nbsp;<code>"Both"</code>&nbsp;。</li>
21+
<li>如果箱子既不是&nbsp;<code>"Bulky"</code>&nbsp;,也不是&nbsp;<code>"Heavy"</code>&nbsp;,那么返回类别为&nbsp;<code>"Neither"</code>&nbsp;。</li>
22+
<li>如果箱子是&nbsp;<code>"Bulky"</code>&nbsp;但不是&nbsp;<code>"Heavy"</code>&nbsp;,那么返回类别为&nbsp;<code>"Bulky"</code>&nbsp;。</li>
23+
<li>如果箱子是&nbsp;<code>"Heavy"</code>&nbsp;但不是&nbsp;<code>"Bulky"</code>&nbsp;,那么返回类别为&nbsp;<code>"Heavy"</code>&nbsp;。</li>
24+
25+
</ul>
26+
27+
<p><strong>注意</strong>,箱子的体积等于箱子的长度、宽度和高度的乘积。</p>
28+
29+
<p>&nbsp;</p>
30+
31+
<p><strong>示例 1:</strong></p>
32+
33+
<pre>
34+
<b>输入:</b>length = 1000, width = 35, height = 700, mass = 300
35+
<b>输出:</b>"Heavy"
36+
<b>解释:</b>
37+
箱子没有任何维度大于等于 10<sup>4 </sup>。
38+
体积为 24500000 &lt;= 10<sup>9 </sup>。所以不能归类为 "Bulky" 。
39+
但是质量 &gt;= 100 ,所以箱子是 "Heavy" 的。
40+
由于箱子不是 "Bulky" 但是是 "Heavy" ,所以我们返回 "Heavy" 。</pre>
41+
42+
<p><strong>示例 2:</strong></p>
43+
44+
<pre>
45+
<b>输入:</b>length = 200, width = 50, height = 800, mass = 50
46+
<b>输出:</b>"Neither"
47+
<b>解释:</b>
48+
箱子没有任何维度大于等于 10<sup>4</sup>&nbsp;
49+
体积为 8 * 10<sup>6</sup> &lt;= 10<sup>9</sup>&nbsp;。所以不能归类为 "Bulky" 。
50+
质量小于 100 ,所以不能归类为 "Heavy" 。
51+
由于不属于上述两者任何一类,所以我们返回 "Neither" 。</pre>
52+
53+
<p>&nbsp;</p>
54+
55+
<p><strong>提示:</strong></p>
56+
57+
<ul>
58+
<li><code>1 &lt;= length, width, height &lt;= 10<sup>5</sup></code></li>
59+
<li><code>1 &lt;= mass &lt;= 10<sup>3</sup></code></li>
60+
</ul>
61+
62+
## 解法
63+
64+
<!-- 这里可写通用的实现逻辑 -->
65+
66+
<!-- tabs:start -->
67+
68+
### **Python3**
69+
70+
<!-- 这里可写当前语言的特殊实现逻辑 -->
71+
72+
```python
73+
class Solution:
74+
def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
75+
v = length * width * height
76+
bulky = int(any(x >= 10000 for x in (
77+
length, width, height)) or v >= 10**9)
78+
heavy = int(mass >= 100)
79+
i = heavy << 1 | bulky
80+
d = ['Neither', 'Bulky', 'Heavy', 'Both']
81+
return d[i]
82+
```
83+
84+
### **Java**
85+
86+
<!-- 这里可写当前语言的特殊实现逻辑 -->
87+
88+
```java
89+
class Solution {
90+
public String categorizeBox(int length, int width, int height, int mass) {
91+
long v = (long) length * width * height;
92+
int bulky = length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 ? 1 : 0;
93+
int heavy = mass >= 100 ? 1 : 0;
94+
String[] d = {"Neither", "Bulky", "Heavy", "Both"};
95+
int i = heavy << 1 | bulky;
96+
return d[i];
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
string categorizeBox(int length, int width, int height, int mass) {
107+
long v = (long) length * width * height;
108+
int bulky = length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 ? 1 : 0;
109+
int heavy = mass >= 100 ? 1 : 0;
110+
string d[4] = {"Neither", "Bulky", "Heavy", "Both"};
111+
int i = heavy << 1 | bulky;
112+
return d[i];
113+
}
114+
};
115+
```
116+
117+
### **Go**
118+
119+
```go
120+
func categorizeBox(length int, width int, height int, mass int) string {
121+
v := length * width * height
122+
i := 0
123+
if length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 {
124+
i |= 1
125+
}
126+
if mass >= 100 {
127+
i |= 2
128+
}
129+
d := [4]string{"Neither", "Bulky", "Heavy", "Both"}
130+
return d[i]
131+
}
132+
```
133+
134+
### **...**
135+
136+
```
137+
138+
```
139+
140+
<!-- tabs:end -->
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# [2525. Categorize Box According to Criteria](https://leetcode.com/problems/categorize-box-according-to-criteria)
2+
3+
[中文文档](/solution/2500-2599/2525.Categorize%20Box%20According%20to%20Criteria/README.md)
4+
5+
## Description
6+
7+
<p>Given four integers <code>length</code>, <code>width</code>, <code>height</code>, and <code>mass</code>, representing the dimensions and mass of a box, respectively, return <em>a string representing the <strong>category</strong> of the box</em>.</p>
8+
9+
<ul>
10+
<li>The box is <code>&quot;Bulky&quot;</code> if:
11+
12+
<ul>
13+
<li><strong>Any</strong> of the dimensions of the box is greater or equal to <code>10<sup>4</sup></code>.</li>
14+
<li>Or, the <strong>volume</strong> of the box is greater or equal to <code>10<sup>9</sup></code>.</li>
15+
</ul>
16+
</li>
17+
<li>If the mass of the box is greater or equal to <code>100</code>, it is <code>&quot;Heavy&quot;.</code></li>
18+
<li>If the box is both <code>&quot;Bulky&quot;</code> and <code>&quot;Heavy&quot;</code>, then its category is <code>&quot;Both&quot;</code>.</li>
19+
<li>If the box is neither <code>&quot;Bulky&quot;</code> nor <code>&quot;Heavy&quot;</code>, then its category is <code>&quot;Neither&quot;</code>.</li>
20+
<li>If the box is <code>&quot;Bulky&quot;</code> but not <code>&quot;Heavy&quot;</code>, then its category is <code>&quot;Bulky&quot;</code>.</li>
21+
<li>If the box is <code>&quot;Heavy&quot;</code> but not <code>&quot;Bulky&quot;</code>, then its category is <code>&quot;Heavy&quot;</code>.</li>
22+
23+
</ul>
24+
25+
<p><strong>Note</strong> that the volume of the box is the product of its length, width and height.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong class="example">Example 1:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> length = 1000, width = 35, height = 700, mass = 300
32+
<strong>Output:</strong> &quot;Heavy&quot;
33+
<strong>Explanation:</strong>
34+
None of the dimensions of the box is greater or equal to 10<sup>4</sup>.
35+
Its volume = 24500000 &lt;= 10<sup>9</sup>. So it cannot be categorized as &quot;Bulky&quot;.
36+
However mass &gt;= 100, so the box is &quot;Heavy&quot;.
37+
Since the box is not &quot;Bulky&quot; but &quot;Heavy&quot;, we return &quot;Heavy&quot;.</pre>
38+
39+
<p><strong class="example">Example 2:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> length = 200, width = 50, height = 800, mass = 50
43+
<strong>Output:</strong> &quot;Neither&quot;
44+
<strong>Explanation:</strong>
45+
None of the dimensions of the box is greater or equal to 10<sup>4</sup>.
46+
Its volume = 8 * 10<sup>6</sup> &lt;= 10<sup>9</sup>. So it cannot be categorized as &quot;Bulky&quot;.
47+
Its mass is also less than 100, so it cannot be categorized as &quot;Heavy&quot; either.
48+
Since its neither of the two above categories, we return &quot;Neither&quot;.</pre>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= length, width, height &lt;= 10<sup>5</sup></code></li>
55+
<li><code>1 &lt;= mass &lt;= 10<sup>3</sup></code></li>
56+
</ul>
57+
58+
## Solutions
59+
60+
<!-- tabs:start -->
61+
62+
### **Python3**
63+
64+
```python
65+
class Solution:
66+
def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
67+
v = length * width * height
68+
bulky = int(any(x >= 10000 for x in (
69+
length, width, height)) or v >= 10**9)
70+
heavy = int(mass >= 100)
71+
i = heavy << 1 | bulky
72+
d = ['Neither', 'Bulky', 'Heavy', 'Both']
73+
return d[i]
74+
```
75+
76+
### **Java**
77+
78+
```java
79+
class Solution {
80+
public String categorizeBox(int length, int width, int height, int mass) {
81+
long v = (long) length * width * height;
82+
int bulky = length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 ? 1 : 0;
83+
int heavy = mass >= 100 ? 1 : 0;
84+
String[] d = {"Neither", "Bulky", "Heavy", "Both"};
85+
int i = heavy << 1 | bulky;
86+
return d[i];
87+
}
88+
}
89+
```
90+
91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
string categorizeBox(int length, int width, int height, int mass) {
97+
long v = (long) length * width * height;
98+
int bulky = length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 ? 1 : 0;
99+
int heavy = mass >= 100 ? 1 : 0;
100+
string d[4] = {"Neither", "Bulky", "Heavy", "Both"};
101+
int i = heavy << 1 | bulky;
102+
return d[i];
103+
}
104+
};
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func categorizeBox(length int, width int, height int, mass int) string {
111+
v := length * width * height
112+
i := 0
113+
if length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 {
114+
i |= 1
115+
}
116+
if mass >= 100 {
117+
i |= 2
118+
}
119+
d := [4]string{"Neither", "Bulky", "Heavy", "Both"}
120+
return d[i]
121+
}
122+
```
123+
124+
### **...**
125+
126+
```
127+
128+
```
129+
130+
<!-- tabs:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
string categorizeBox(int length, int width, int height, int mass) {
4+
long v = (long) length * width * height;
5+
int bulky = length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 ? 1 : 0;
6+
int heavy = mass >= 100 ? 1 : 0;
7+
string d[4] = {"Neither", "Bulky", "Heavy", "Both"};
8+
int i = heavy << 1 | bulky;
9+
return d[i];
10+
}
11+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func categorizeBox(length int, width int, height int, mass int) string {
2+
v := length * width * height
3+
i := 0
4+
if length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 {
5+
i |= 1
6+
}
7+
if mass >= 100 {
8+
i |= 2
9+
}
10+
d := [4]string{"Neither", "Bulky", "Heavy", "Both"}
11+
return d[i]
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public String categorizeBox(int length, int width, int height, int mass) {
3+
long v = (long) length * width * height;
4+
int bulky = length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 ? 1 : 0;
5+
int heavy = mass >= 100 ? 1 : 0;
6+
String[] d = {"Neither", "Bulky", "Heavy", "Both"};
7+
int i = heavy << 1 | bulky;
8+
return d[i];
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:
3+
v = length * width * height
4+
bulky = int(any(x >= 10000 for x in (
5+
length, width, height)) or v >= 10**9)
6+
heavy = int(mass >= 100)
7+
i = heavy << 1 | bulky
8+
d = ['Neither', 'Bulky', 'Heavy', 'Both']
9+
return d[i]

0 commit comments

Comments
 (0)