Skip to content

Commit 07f736b

Browse files
committed
feat: add new lc problems
1 parent b37e41c commit 07f736b

File tree

21 files changed

+918
-26
lines changed

21 files changed

+918
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [2263. Make Array Non-decreasing or Non-increasing](https://leetcode.cn/problems/make-array-non-decreasing-or-non-increasing)
2+
3+
[English Version](/solution/2200-2299/2263.Make%20Array%20Non-decreasing%20or%20Non-increasing/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, you can:</p>
10+
11+
<ul>
12+
<li>Choose an index <code>i</code> in the range <code>0 &lt;= i &lt; nums.length</code></li>
13+
<li>Set <code>nums[i]</code> to <code>nums[i] + 1</code> <strong>or</strong> <code>nums[i] - 1</code></li>
14+
</ul>
15+
16+
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> <strong>non-decreasing</strong> or <strong>non-increasing</strong>.</em></p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> nums = [3,2,4,5,0]
23+
<strong>Output:</strong> 4
24+
<strong>Explanation:</strong>
25+
One possible way to turn nums into non-increasing order is to:
26+
- Add 1 to nums[1] once so that it becomes 3.
27+
- Subtract 1 from nums[2] once so it becomes 3.
28+
- Subtract 1 from nums[3] twice so it becomes 3.
29+
After doing the 4 operations, nums becomes [3,3,3,3,0] which is in non-increasing order.
30+
Note that it is also possible to turn nums into [4,4,4,4,0] in 4 operations.
31+
It can be proven that 4 is the minimum number of operations needed.
32+
</pre>
33+
34+
<p><strong>Example 2:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> nums = [2,2,3,4]
38+
<strong>Output:</strong> 0
39+
<strong>Explanation:</strong> nums is already in non-decreasing order, so no operations are needed and we return 0.
40+
</pre>
41+
42+
<p><strong>Example 3:</strong></p>
43+
44+
<pre>
45+
<strong>Input:</strong> nums = [0]
46+
<strong>Output:</strong> 0
47+
<strong>Explanation:</strong> nums is already in non-decreasing order, so no operations are needed and we return 0.
48+
</pre>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
55+
<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
56+
</ul>
57+
58+
## 解法
59+
60+
<!-- 这里可写通用的实现逻辑 -->
61+
62+
<!-- tabs:start -->
63+
64+
### **Python3**
65+
66+
<!-- 这里可写当前语言的特殊实现逻辑 -->
67+
68+
```python
69+
70+
```
71+
72+
### **Java**
73+
74+
<!-- 这里可写当前语言的特殊实现逻辑 -->
75+
76+
```java
77+
78+
```
79+
80+
### **TypeScript**
81+
82+
```ts
83+
84+
```
85+
86+
### **...**
87+
88+
```
89+
90+
```
91+
92+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [2263. Make Array Non-decreasing or Non-increasing](https://leetcode.com/problems/make-array-non-decreasing-or-non-increasing)
2+
3+
[中文文档](/solution/2200-2299/2263.Make%20Array%20Non-decreasing%20or%20Non-increasing/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, you can:</p>
8+
9+
<ul>
10+
<li>Choose an index <code>i</code> in the range <code>0 &lt;= i &lt; nums.length</code></li>
11+
<li>Set <code>nums[i]</code> to <code>nums[i] + 1</code> <strong>or</strong> <code>nums[i] - 1</code></li>
12+
</ul>
13+
14+
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> <strong>non-decreasing</strong> or <strong>non-increasing</strong>.</em></p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong>Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> nums = [3,2,4,5,0]
21+
<strong>Output:</strong> 4
22+
<strong>Explanation:</strong>
23+
One possible way to turn nums into non-increasing order is to:
24+
- Add 1 to nums[1] once so that it becomes 3.
25+
- Subtract 1 from nums[2] once so it becomes 3.
26+
- Subtract 1 from nums[3] twice so it becomes 3.
27+
After doing the 4 operations, nums becomes [3,3,3,3,0] which is in non-increasing order.
28+
Note that it is also possible to turn nums into [4,4,4,4,0] in 4 operations.
29+
It can be proven that 4 is the minimum number of operations needed.
30+
</pre>
31+
32+
<p><strong>Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> nums = [2,2,3,4]
36+
<strong>Output:</strong> 0
37+
<strong>Explanation:</strong> nums is already in non-decreasing order, so no operations are needed and we return 0.
38+
</pre>
39+
40+
<p><strong>Example 3:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> nums = [0]
44+
<strong>Output:</strong> 0
45+
<strong>Explanation:</strong> nums is already in non-decreasing order, so no operations are needed and we return 0.
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
53+
<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
54+
</ul>
55+
56+
## Solutions
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
```python
63+
64+
```
65+
66+
### **Java**
67+
68+
```java
69+
70+
```
71+
72+
### **TypeScript**
73+
74+
```ts
75+
76+
```
77+
78+
### **...**
79+
80+
```
81+
82+
```
83+
84+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# [2264. 字符串中最大的 3 位相同数字](https://leetcode.cn/problems/largest-3-same-digit-number-in-string)
2+
3+
[English Version](/solution/2200-2299/2264.Largest%203-Same-Digit%20Number%20in%20String/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个字符串 <code>num</code> ,表示一个大整数。如果一个整数满足下述所有条件,则认为该整数是一个 <strong>优质整数</strong> :</p>
10+
11+
<ul>
12+
<li>该整数是 <code>num</code> 的一个长度为 <code>3</code> 的 <strong>子字符串</strong> 。</li>
13+
<li>该整数由唯一一个数字重复 <code>3</code> 次组成。</li>
14+
</ul>
15+
16+
<p>以字符串形式返回 <strong>最大的优质整数</strong> 。如果不存在满足要求的整数,则返回一个空字符串 <code>""</code> 。</p>
17+
18+
<p><strong>注意:</strong></p>
19+
20+
<ul>
21+
<li><strong>子字符串</strong> 是字符串中的一个连续字符序列。</li>
22+
<li><code>num</code> 或优质整数中可能存在 <strong>前导零</strong> 。</li>
23+
</ul>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong>示例 1:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>num = "6<em><strong>777</strong></em>133339"
31+
<strong>输出:</strong>"777"
32+
<strong>解释:</strong>num 中存在两个优质整数:"777" 和 "333" 。
33+
"777" 是最大的那个,所以返回 "777" 。
34+
</pre>
35+
36+
<p><strong>示例 2:</strong></p>
37+
38+
<pre>
39+
<strong>输入:</strong>num = "23<em><strong>000</strong></em>19"
40+
<strong>输出:</strong>"000"
41+
<strong>解释:</strong>"000" 是唯一一个优质整数。
42+
</pre>
43+
44+
<p><strong>示例 3:</strong></p>
45+
46+
<pre>
47+
<strong>输入:</strong>num = "42352338"
48+
<strong>输出:</strong>""
49+
<strong>解释:</strong>不存在长度为 3 且仅由一个唯一数字组成的整数。因此,不存在优质整数。
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
54+
<p><strong>提示:</strong></p>
55+
56+
<ul>
57+
<li><code>3 &lt;= num.length &lt;= 1000</code></li>
58+
<li><code>num</code> 仅由数字(<code>0</code> - <code>9</code>)组成</li>
59+
</ul>
60+
61+
## 解法
62+
63+
<!-- 这里可写通用的实现逻辑 -->
64+
65+
<!-- tabs:start -->
66+
67+
### **Python3**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```python
72+
73+
```
74+
75+
### **Java**
76+
77+
<!-- 这里可写当前语言的特殊实现逻辑 -->
78+
79+
```java
80+
81+
```
82+
83+
### **TypeScript**
84+
85+
```ts
86+
87+
```
88+
89+
### **...**
90+
91+
```
92+
93+
```
94+
95+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [2264. Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string)
2+
3+
[中文文档](/solution/2200-2299/2264.Largest%203-Same-Digit%20Number%20in%20String/README.md)
4+
5+
## Description
6+
7+
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p>
8+
9+
<ul>
10+
<li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li>
11+
<li>It consists of only one unique digit.</li>
12+
</ul>
13+
14+
<p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p>
15+
16+
<p>Note:</p>
17+
18+
<ul>
19+
<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
20+
<li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li>
21+
</ul>
22+
23+
<p>&nbsp;</p>
24+
<p><strong>Example 1:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot;
28+
<strong>Output:</strong> &quot;777&quot;
29+
<strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;.
30+
&quot;777&quot; is the largest, so we return &quot;777&quot;.
31+
</pre>
32+
33+
<p><strong>Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot;
37+
<strong>Output:</strong> &quot;000&quot;
38+
<strong>Explanation:</strong> &quot;000&quot; is the only good integer.
39+
</pre>
40+
41+
<p><strong>Example 3:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> num = &quot;42352338&quot;
45+
<strong>Output:</strong> &quot;&quot;
46+
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>3 &lt;= num.length &lt;= 1000</code></li>
54+
<li><code>num</code> only consists of digits.</li>
55+
</ul>
56+
57+
## Solutions
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
```java
70+
71+
```
72+
73+
### **TypeScript**
74+
75+
```ts
76+
77+
```
78+
79+
### **...**
80+
81+
```
82+
83+
```
84+
85+
<!-- tabs:end -->

0 commit comments

Comments
 (0)