Skip to content

Commit 8044c8a

Browse files
authored
feat: add new lc problems: No.2894~2897 (#1764)
* No.2894.Divisible and Non-divisible Sums Difference * No.2895.Minimum Processing Time * No.2896.Apply Operations to Make Two Strings Equal * No.2897.Apply Operations on Array to Maximize Sum of Squares
1 parent 67032f8 commit 8044c8a

File tree

14 files changed

+760
-0
lines changed

14 files changed

+760
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# [2894. 分类求和并作差](https://leetcode.cn/problems/divisible-and-non-divisible-sums-difference)
2+
3+
[English Version](/solution/2800-2899/2894.Divisible%20and%20Non-divisible%20Sums%20Difference/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你两个正整数 <code>n</code> 和 <code>m</code> 。</p>
10+
11+
<p>现定义两个整数 <code>num1</code> 和 <code>num2</code> ,如下所示:</p>
12+
13+
<ul>
14+
<li><code>num1</code>:范围 <code>[1, n]</code> 内所有 <strong>无法被 </strong><code>m</code><strong> 整除</strong> 的整数之和。</li>
15+
<li><code>num2</code>:范围 <code>[1, n]</code> 内所有 <strong>能够被 </strong><code>m</code><strong> 整除</strong> 的整数之和。</li>
16+
</ul>
17+
18+
<p>返回整数 <code>num1 - num2</code> 。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong class="example">示例 1:</strong></p>
23+
24+
<pre>
25+
<strong>输入:</strong>n = 10, m = 3
26+
<strong>输出:</strong>19
27+
<strong>解释:</strong>在这个示例中:
28+
- 范围 [1, 10] 内无法被 3 整除的整数为 [1,2,4,5,7,8,10] ,num1 = 这些整数之和 = 37 。
29+
- 范围 [1, 10] 内能够被 3 整除的整数为 [3,6,9] ,num2 = 这些整数之和 = 18 。
30+
返回 37 - 18 = 19 作为答案。
31+
</pre>
32+
33+
<p><strong class="example">示例 2:</strong></p>
34+
35+
<pre>
36+
<strong>输入:</strong>n = 5, m = 6
37+
<strong>输出:</strong>15
38+
<strong>解释:</strong>在这个示例中:
39+
- 范围 [1, 5] 内无法被 6 整除的整数为 [1,2,3,4,5] ,num1 = 这些整数之和 = 15 。
40+
- 范围 [1, 5] 内能够被 6 整除的整数为 [] ,num2 = 这些整数之和 = 0 。
41+
返回 15 - 0 = 15 作为答案。
42+
</pre>
43+
44+
<p><strong class="example">示例 3:</strong></p>
45+
46+
<pre>
47+
<strong>输入:</strong>n = 5, m = 1
48+
<strong>输出:</strong>-15
49+
<strong>解释:</strong>在这个示例中:
50+
- 范围 [1, 5] 内无法被 1 整除的整数为 [] ,num1 = 这些整数之和 = 0 。
51+
- 范围 [1, 5] 内能够被 1 整除的整数为 [1,2,3,4,5] ,num2 = 这些整数之和 = 15 。
52+
返回 0 - 15 = -15 作为答案。
53+
</pre>
54+
55+
<p>&nbsp;</p>
56+
57+
<p><strong>提示:</strong></p>
58+
59+
<ul>
60+
<li><code>1 &lt;= n, m &lt;= 1000</code></li>
61+
</ul>
62+
63+
## 解法
64+
65+
<!-- 这里可写通用的实现逻辑 -->
66+
67+
<!-- tabs:start -->
68+
69+
### **Python3**
70+
71+
<!-- 这里可写当前语言的特殊实现逻辑 -->
72+
73+
```python
74+
75+
```
76+
77+
### **Java**
78+
79+
<!-- 这里可写当前语言的特殊实现逻辑 -->
80+
81+
```java
82+
83+
```
84+
85+
### **C++**
86+
87+
```cpp
88+
89+
```
90+
91+
### **Go**
92+
93+
```go
94+
95+
```
96+
97+
### **...**
98+
99+
```
100+
101+
```
102+
103+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [2894. Divisible and Non-divisible Sums Difference](https://leetcode.com/problems/divisible-and-non-divisible-sums-difference)
2+
3+
[中文文档](/solution/2800-2899/2894.Divisible%20and%20Non-divisible%20Sums%20Difference/README.md)
4+
5+
## Description
6+
7+
<p>You are given positive integers <code>n</code> and <code>m</code>.</p>
8+
9+
<p>Define two integers, <code>num1</code> and <code>num2</code>, as follows:</p>
10+
11+
<ul>
12+
<li><code>num1</code>: The sum of all integers in the range <code>[1, n]</code> that are <strong>not divisible</strong> by <code>m</code>.</li>
13+
<li><code>num2</code>: The sum of all integers in the range <code>[1, n]</code> that are <strong>divisible</strong> by <code>m</code>.</li>
14+
</ul>
15+
16+
<p>Return <em>the integer</em> <code>num1 - num2</code>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> n = 10, m = 3
23+
<strong>Output:</strong> 19
24+
<strong>Explanation:</strong> In the given example:
25+
- Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37.
26+
- Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18.
27+
We return 37 - 18 = 19 as the answer.
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> n = 5, m = 6
34+
<strong>Output:</strong> 15
35+
<strong>Explanation:</strong> In the given example:
36+
- Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15.
37+
- Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0.
38+
We return 15 - 0 = 15 as the answer.
39+
</pre>
40+
41+
<p><strong class="example">Example 3:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> n = 5, m = 1
45+
<strong>Output:</strong> -15
46+
<strong>Explanation:</strong> In the given example:
47+
- Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0.
48+
- Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15.
49+
We return 0 - 15 = -15 as the answer.
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
<p><strong>Constraints:</strong></p>
54+
55+
<ul>
56+
<li><code>1 &lt;= n, m &lt;= 1000</code></li>
57+
</ul>
58+
59+
## Solutions
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
```python
66+
67+
```
68+
69+
### **Java**
70+
71+
```java
72+
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
79+
```
80+
81+
### **Go**
82+
83+
```go
84+
85+
```
86+
87+
### **...**
88+
89+
```
90+
91+
```
92+
93+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [2895. 最小处理时间](https://leetcode.cn/problems/minimum-processing-time)
2+
3+
[English Version](/solution/2800-2899/2895.Minimum%20Processing%20Time/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>你有 <code>n</code> 颗处理器,每颗处理器都有 <code>4</code> 个核心。现有 <code>n * 4</code> 个待执行任务,每个核心只执行 <strong>一个</strong> 任务。</p>
10+
11+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>processorTime</code> ,表示每颗处理器最早空闲时间。另给你一个下标从 <strong>0</strong> 开始的整数数组 <code>tasks</code> ,表示执行每个任务所需的时间。返回所有任务都执行完毕需要的 <strong>最小时间</strong> 。</p>
12+
13+
<p>注意:每个核心独立执行任务。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<pre>
20+
<strong>输入:</strong>processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5]
21+
<strong>输出:</strong>16
22+
<strong>解释:</strong>
23+
最优的方案是将下标为 4, 5, 6, 7 的任务分配给第一颗处理器(最早空闲时间 time = 8),下标为 0, 1, 2, 3 的任务分配给第二颗处理器(最早空闲时间 time = 10)。
24+
第一颗处理器执行完所有任务需要花费的时间 = max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16 。
25+
第二颗处理器执行完所有任务需要花费的时间 = max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13 。
26+
因此,可以证明执行完所有任务需要花费的最小时间是 16 。</pre>
27+
28+
<p><strong>示例 2:</strong></p>
29+
30+
<pre>
31+
<strong>输入:</strong>processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3]
32+
<strong>输出:</strong>23
33+
<strong>解释:</strong>
34+
最优的方案是将下标为 1, 4, 5, 6 的任务分配给第一颗处理器(最早空闲时间 time = 10),下标为 0, 2, 3, 7 的任务分配给第二颗处理器(最早空闲时间 time = 20)。
35+
第一颗处理器执行完所有任务需要花费的时间 = max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18 。
36+
第二颗处理器执行完所有任务需要花费的时间 = max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23 。
37+
因此,可以证明执行完所有任务需要花费的最小时间是 23 。
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
42+
<p><strong>提示:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= n == processorTime.length &lt;= 25000</code></li>
46+
<li><code>1 &lt;= tasks.length &lt;= 10<sup>5</sup></code></li>
47+
<li><code>0 &lt;= processorTime[i] &lt;= 10<sup>9</sup></code></li>
48+
<li><code>1 &lt;= tasks[i] &lt;= 10<sup>9</sup></code></li>
49+
<li><code>tasks.length == 4 * n</code></li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
<!-- 这里可写当前语言的特殊实现逻辑 -->
61+
62+
```python
63+
64+
```
65+
66+
### **Java**
67+
68+
<!-- 这里可写当前语言的特殊实现逻辑 -->
69+
70+
```java
71+
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
78+
```
79+
80+
### **Go**
81+
82+
```go
83+
84+
```
85+
86+
### **...**
87+
88+
```
89+
90+
```
91+
92+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [2895. Minimum Processing Time](https://leetcode.com/problems/minimum-processing-time)
2+
3+
[中文文档](/solution/2800-2899/2895.Minimum%20Processing%20Time/README.md)
4+
5+
## Description
6+
7+
<p>You have <code>n</code> processors each having <code>4</code> cores and <code>n * 4</code> tasks that need to be executed such that each core should perform only <strong>one</strong> task.</p>
8+
9+
<p>Given a <strong>0-indexed</strong> integer array <code>processorTime</code> representing the time at which each processor becomes available for the first time and a <strong>0-indexed </strong>integer array <code>tasks</code> representing the time it takes to execute each task, return <em>the <strong>minimum</strong> time when all of the tasks have been executed by the processors.</em></p>
10+
11+
<p><strong>Note: </strong>Each core executes the task independently of the others.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5]
18+
<strong>Output:</strong> 16
19+
<strong>Explanation:</strong>
20+
It&#39;s optimal to assign the tasks at indexes 4, 5, 6, 7 to the first processor which becomes available at time = 8, and the tasks at indexes 0, 1, 2, 3 to the second processor which becomes available at time = 10.
21+
Time taken by the first processor to finish execution of all tasks = max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16.
22+
Time taken by the second processor to finish execution of all tasks = max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13.
23+
Hence, it can be shown that the minimum time taken to execute all the tasks is 16.</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3]
29+
<strong>Output:</strong> 23
30+
<strong>Explanation:</strong>
31+
It&#39;s optimal to assign the tasks at indexes 1, 4, 5, 6 to the first processor which becomes available at time = 10, and the tasks at indexes 0, 2, 3, 7 to the second processor which becomes available at time = 20.
32+
Time taken by the first processor to finish execution of all tasks = max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18.
33+
Time taken by the second processor to finish execution of all tasks = max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23.
34+
Hence, it can be shown that the minimum time taken to execute all the tasks is 23.
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= n == processorTime.length &lt;= 25000</code></li>
42+
<li><code>1 &lt;= tasks.length &lt;= 10<sup>5</sup></code></li>
43+
<li><code>0 &lt;= processorTime[i] &lt;= 10<sup>9</sup></code></li>
44+
<li><code>1 &lt;= tasks[i] &lt;= 10<sup>9</sup></code></li>
45+
<li><code>tasks.length == 4 * n</code></li>
46+
</ul>
47+
48+
## Solutions
49+
50+
<!-- tabs:start -->
51+
52+
### **Python3**
53+
54+
```python
55+
56+
```
57+
58+
### **Java**
59+
60+
```java
61+
62+
```
63+
64+
### **C++**
65+
66+
```cpp
67+
68+
```
69+
70+
### **Go**
71+
72+
```go
73+
74+
```
75+
76+
### **...**
77+
78+
```
79+
80+
```
81+
82+
<!-- tabs:end -->

0 commit comments

Comments
 (0)