Skip to content

Commit 17dcb2f

Browse files
committed
feat: add solutions to lc problems: No.2460~2463
* No.2460.Apply Operations to an Array * No.2461.Maximum Sum of Distinct Subarrays With Length K * No.2462.Total Cost to Hire K Workers * No.2463.Minimum Total Distance Traveled
1 parent 886885f commit 17dcb2f

File tree

43 files changed

+2178
-31
lines changed

Some content is hidden

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

43 files changed

+2178
-31
lines changed

solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class Solution {
105105
class Solution {
106106
private String s;
107107
private int n;
108-
108+
109109
public String longestPalindrome(String s) {
110110
this.s = s;
111111
n = s.length();
@@ -121,7 +121,7 @@ class Solution {
121121
}
122122
return s.substring(start, start + mx);
123123
}
124-
124+
125125
private int f(int l, int r) {
126126
while (l >= 0 && r < n && s.charAt(l) == s.charAt(r)) {
127127
--l;

solution/0100-0199/0158.Read N Characters Given read4 II - Call Multiple Times/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class Solution:
132132
```java
133133
/**
134134
* The read4 API is defined in the parent class Reader4.
135-
* int read4(char[] buf4);
135+
* int read4(char[] buf4);
136136
*/
137137

138138
public class Solution extends Reader4 {

solution/0100-0199/0158.Read N Characters Given read4 II - Call Multiple Times/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# The read4 API is already defined for you.
22
# def read4(buf4: List[str]) -> int:
33

4+
45
class Solution:
56
def __init__(self):
67
self.buf4 = [None] * 4

solution/0200-0299/0254.Factor Combinations/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def dfs(n, i):
1010
dfs(n // j, j)
1111
t.pop()
1212
j += 1
13+
1314
t = []
1415
ans = []
1516
dfs(n, 2)

solution/0400-0499/0433.Minimum Genetic Mutation/README_EN.md

+6-15
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,40 @@
66

77
<p>A gene string can be represented by an 8-character long string, with choices from <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p>
88

9-
<p>Suppose we need to investigate a mutation from a gene string <code>start</code> to a gene string <code>end</code> where one mutation is defined as one single character changed in the gene string.</p>
9+
<p>Suppose we need to investigate a mutation from a gene string <code>startGene</code> to a gene string <code>endGene</code> where one mutation is defined as one single character changed in the gene string.</p>
1010

1111
<ul>
1212
<li>For example, <code>&quot;AACCGGTT&quot; --&gt; &quot;AACCGGTA&quot;</code> is one mutation.</li>
1313
</ul>
1414

1515
<p>There is also a gene bank <code>bank</code> that records all the valid gene mutations. A gene must be in <code>bank</code> to make it a valid gene string.</p>
1616

17-
<p>Given the two gene strings <code>start</code> and <code>end</code> and the gene bank <code>bank</code>, return <em>the minimum number of mutations needed to mutate from </em><code>start</code><em> to </em><code>end</code>. If there is no such a mutation, return <code>-1</code>.</p>
17+
<p>Given the two gene strings <code>startGene</code> and <code>endGene</code> and the gene bank <code>bank</code>, return <em>the minimum number of mutations needed to mutate from </em><code>startGene</code><em> to </em><code>endGene</code>. If there is no such a mutation, return <code>-1</code>.</p>
1818

1919
<p>Note that the starting point is assumed to be valid, so it might not be included in the bank.</p>
2020

2121
<p>&nbsp;</p>
2222
<p><strong class="example">Example 1:</strong></p>
2323

2424
<pre>
25-
<strong>Input:</strong> start = &quot;AACCGGTT&quot;, end = &quot;AACCGGTA&quot;, bank = [&quot;AACCGGTA&quot;]
25+
<strong>Input:</strong> startGene = &quot;AACCGGTT&quot;, endGene = &quot;AACCGGTA&quot;, bank = [&quot;AACCGGTA&quot;]
2626
<strong>Output:</strong> 1
2727
</pre>
2828

2929
<p><strong class="example">Example 2:</strong></p>
3030

3131
<pre>
32-
<strong>Input:</strong> start = &quot;AACCGGTT&quot;, end = &quot;AAACGGTA&quot;, bank = [&quot;AACCGGTA&quot;,&quot;AACCGCTA&quot;,&quot;AAACGGTA&quot;]
32+
<strong>Input:</strong> startGene = &quot;AACCGGTT&quot;, endGene = &quot;AAACGGTA&quot;, bank = [&quot;AACCGGTA&quot;,&quot;AACCGCTA&quot;,&quot;AAACGGTA&quot;]
3333
<strong>Output:</strong> 2
3434
</pre>
3535

36-
<p><strong class="example">Example 3:</strong></p>
37-
38-
<pre>
39-
<strong>Input:</strong> start = &quot;AAAAACCC&quot;, end = &quot;AACCCCCC&quot;, bank = [&quot;AAAACCCC&quot;,&quot;AAACCCCC&quot;,&quot;AACCCCCC&quot;]
40-
<strong>Output:</strong> 3
41-
</pre>
42-
4336
<p>&nbsp;</p>
4437
<p><strong>Constraints:</strong></p>
4538

4639
<ul>
47-
<li><code>start.length == 8</code></li>
48-
<li><code>end.length == 8</code></li>
4940
<li><code>0 &lt;= bank.length &lt;= 10</code></li>
50-
<li><code>bank[i].length == 8</code></li>
51-
<li><code>start</code>, <code>end</code>, and <code>bank[i]</code> consist of only the characters <code>[&#39;A&#39;, &#39;C&#39;, &#39;G&#39;, &#39;T&#39;]</code>.</li>
41+
<li><code>startGene.length == endGene.length == bank[i].length == 8</code></li>
42+
<li><code>startGene</code>, <code>endGene</code>, and <code>bank[i]</code> consist of only the characters <code>[&#39;A&#39;, &#39;C&#39;, &#39;G&#39;, &#39;T&#39;]</code>.</li>
5243
</ul>
5344

5445
## Solutions

solution/0700-0799/0769.Max Chunks To Make Sorted/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<strong>解释:</strong>
3333
我们可以把它分成两块,例如 [1, 0], [2, 3, 4]。
3434
然而,分成 [1, 0], [2], [3], [4] 可以得到最多的块数。
35+
对每个块单独排序后,结果为 [0, 1], [2], [3], [4]
3536
</pre>
3637

3738
<p>&nbsp;</p>

solution/0700-0799/0779.K-th Symbol in Grammar/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<strong>输出:</strong> 0
3131
<strong>解释:</strong>
3232
第一行: 0
33-
第二行: 0<u>1</u>
33+
第二行: <u>0</u>1
3434
</pre>
3535

3636
<p><strong>示例 3:</strong></p>

solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<li>它可以被写作&nbsp;<code>(A)</code>,其中&nbsp;<code>A</code>&nbsp;是有效字符串。</li>
1515
</ul>
1616

17-
<p>给定一个括号字符串 <code>s</code> ,移动N次,你就可以在字符串的任何位置插入一个括号。</p>
17+
<p>给定一个括号字符串 <code>s</code> ,在每一次操作中,你都可以在字符串的任何位置插入一个括号</p>
1818

1919
<ul>
2020
<li>例如,如果 <code>s = "()))"</code> ,你可以插入一个开始括号为 <code>"(()))"</code> 或结束括号为 <code>"())))"</code> 。</li>

solution/1300-1399/1367.Linked List in Binary Tree/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [1367. 二叉树中的列表](https://leetcode.cn/problems/linked-list-in-binary-tree)
1+
# [1367. 二叉树中的链表](https://leetcode.cn/problems/linked-list-in-binary-tree)
22

33
[English Version](/solution/1300-1399/1367.Linked%20List%20in%20Binary%20Tree/README_EN.md)
44

solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
<p>如果 <code>s</code> 包含 <strong>零个或一个由连续的 <code>'1'</code> 组成的字段</strong> ,返回 <code>true</code>​​​ 。否则,返回 <code>false</code> 。</p>
1212

13-
<p>如果 <code>s</code>&nbsp;&nbsp;<strong>由连续若干个&nbsp;<code>'1'</code> 组成的字段</strong>&nbsp;数量不超过 <code>1</code>,返回 <code>true</code>​​​ 。否则,返回 <code>false</code> 。</p>
14-
1513
<p>&nbsp;</p>
1614

1715
<p><strong>示例 1:</strong></p>

solution/2400-2499/2459.Sort Array by Moving Items to Empty Space/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public:
151151
if (nums[k] != k) cnt -= 2;
152152
return cnt;
153153
};
154-
154+
155155
int a = f(nums, 0);
156156
vector<int> arr = nums;
157157
for (int& v : arr) v = (v - 1 + n) % n;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# [2460. 对数组执行操作](https://leetcode.cn/problems/apply-operations-to-an-array)
2+
3+
[English Version](/solution/2400-2499/2460.Apply%20Operations%20to%20an%20Array/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的数组 <code>nums</code> ,数组大小为 <code>n</code> ,且由 <strong>非负</strong> 整数组成。</p>
10+
11+
<p>你需要对数组执行 <code>n - 1</code> 步操作,其中第 <code>i</code> 步操作(从 <strong>0</strong> 开始计数)要求对 <code>nums</code> 中第 <code>i</code> 个元素执行下述指令:</p>
12+
13+
<ul>
14+
<li>如果 <code>nums[i] == nums[i + 1]</code> ,则 <code>nums[i]</code> 的值变成原来的 <code>2</code> 倍,<code>nums[i + 1]</code> 的值变成 <code>0</code> 。否则,跳过这步操作。</li>
15+
</ul>
16+
17+
<p>在执行完 <strong>全部</strong> 操作后,将所有 <code>0</code> <strong>移动</strong> 到数组的 <strong>末尾</strong> 。</p>
18+
19+
<ul>
20+
<li>例如,数组 <code>[1,0,2,0,0,1]</code> 将所有 <code>0</code> 移动到末尾后变为 <code>[1,2,1,0,0,0]</code> 。</li>
21+
</ul>
22+
23+
<p>返回结果数组。</p>
24+
25+
<p><strong>注意</strong> 操作应当 <strong>依次有序</strong> 执行,而不是一次性全部执行。</p>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong>示例 1:</strong></p>
30+
31+
<pre>
32+
<strong>输入:</strong>nums = [1,2,2,1,1,0]
33+
<strong>输出:</strong>[1,4,2,0,0,0]
34+
<strong>解释:</strong>执行以下操作:
35+
- i = 0: nums[0] 和 nums[1] 不相等,跳过这步操作。
36+
- i = 1: nums[1] 和 nums[2] 相等,nums[1] 的值变成原来的 2 倍,nums[2] 的值变成 0 。数组变成 [1,<em><strong>4</strong></em>,<em><strong>0</strong></em>,1,1,0] 。
37+
- i = 2: nums[2] 和 nums[3] 不相等,所以跳过这步操作。
38+
- i = 3: nums[3] 和 nums[4] 相等,nums[3] 的值变成原来的 2 倍,nums[4] 的值变成 0 。数组变成 [1,4,0,<em><strong>2</strong></em>,<em><strong>0</strong></em>,0] 。
39+
- i = 4: nums[4] 和 nums[5] 相等,nums[4] 的值变成原来的 2 倍,nums[5] 的值变成 0 。数组变成 [1,4,0,2,<em><strong>0</strong></em>,<em><strong>0</strong></em>] 。
40+
执行完所有操作后,将 0 全部移动到数组末尾,得到结果数组 [1,4,2,0,0,0] 。
41+
</pre>
42+
43+
<p><strong>示例 2:</strong></p>
44+
45+
<pre>
46+
<strong>输入:</strong>nums = [0,1]
47+
<strong>输出:</strong>[1,0]
48+
<strong>解释:</strong>无法执行任何操作,只需要将 0 移动到末尾。
49+
</pre>
50+
51+
<p>&nbsp;</p>
52+
53+
<p><strong>提示:</strong></p>
54+
55+
<ul>
56+
<li><code>2 &lt;= nums.length &lt;= 2000</code></li>
57+
<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
58+
</ul>
59+
60+
## 解法
61+
62+
<!-- 这里可写通用的实现逻辑 -->
63+
64+
**方法一:模拟**
65+
66+
直接根据题意模拟即可。
67+
68+
时间复杂度 $O(n)$,忽略答案的空间消耗,空间复杂度 $O(1)$。其中 $n$ 是数组 `nums` 的长度。
69+
70+
<!-- tabs:start -->
71+
72+
### **Python3**
73+
74+
<!-- 这里可写当前语言的特殊实现逻辑 -->
75+
76+
```python
77+
class Solution:
78+
def applyOperations(self, nums: List[int]) -> List[int]:
79+
n = len(nums)
80+
for i in range(n - 1):
81+
if nums[i] == nums[i + 1]:
82+
nums[i] <<= 1
83+
nums[i + 1] = 0
84+
ans = [v for v in nums if v]
85+
ans += [0] * (n - len(ans))
86+
return ans
87+
```
88+
89+
### **Java**
90+
91+
<!-- 这里可写当前语言的特殊实现逻辑 -->
92+
93+
```java
94+
class Solution {
95+
public int[] applyOperations(int[] nums) {
96+
int n = nums.length;
97+
for (int i = 0; i < n - 1; ++i) {
98+
if (nums[i] == nums[i + 1]) {
99+
nums[i] <<= 1;
100+
nums[i + 1] = 0;
101+
}
102+
}
103+
int[] ans = new int[n];
104+
int j = 0;
105+
for (int i = 0; i < n; ++i) {
106+
if (nums[i] != 0) {
107+
ans[j++] = nums[i];
108+
}
109+
}
110+
for (int i = 0; i < n; ++i) {
111+
if (nums[i] == 0) {
112+
ans[j++] = nums[i];
113+
}
114+
}
115+
return ans;
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
vector<int> applyOperations(vector<int>& nums) {
126+
int n = nums.size();
127+
for (int i = 0; i < n - 1; ++i) {
128+
if (nums[i] == nums[i + 1]) {
129+
nums[i] <<= 1;
130+
nums[i + 1] = 0;
131+
}
132+
}
133+
vector<int> ans(n);
134+
int j = 0;
135+
for (int i = 0; i < n; ++i) if (nums[i]) ans[j++] = nums[i];
136+
for (int i = 0; i < n; ++i) if (!nums[i]) ans[j++] = nums[i];
137+
return ans;
138+
}
139+
};
140+
```
141+
142+
### **Go**
143+
144+
```go
145+
func applyOperations(nums []int) (ans []int) {
146+
n := len(nums)
147+
for i := 0; i < n - 1; i++ {
148+
if nums[i] == nums[i + 1] {
149+
nums[i] <<= 1
150+
nums[i + 1] = 0
151+
}
152+
}
153+
for _, v := range nums {
154+
if v != 0 {
155+
ans = append(ans, v)
156+
}
157+
}
158+
for _, v := range nums {
159+
if v == 0 {
160+
ans = append(ans, v)
161+
}
162+
}
163+
return
164+
}
165+
```
166+
167+
### **TypeScript**
168+
169+
```ts
170+
171+
```
172+
173+
### **...**
174+
175+
```
176+
177+
```
178+
179+
<!-- tabs:end -->

0 commit comments

Comments
 (0)