Skip to content

Commit db3e63c

Browse files
authored
feat: add solutions to lc problems: No.2894~2897 (#1765)
* 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 8044c8a commit db3e63c

File tree

28 files changed

+1217
-22
lines changed

28 files changed

+1217
-22
lines changed

solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README.md

+51-3
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,82 @@
6464

6565
<!-- 这里可写通用的实现逻辑 -->
6666

67+
**方法一:模拟**
68+
69+
我们遍历区间 $[1, n]$ 中的每一个数,如果它能被 $m$ 整除,那么答案就减去这个数,否则答案就加上这个数。
70+
71+
遍历结束后,返回答案即可。
72+
73+
时间复杂度 $O(n)$,其中 $n$ 是题目给定的整数。空间复杂度 $O(1)$。
74+
6775
<!-- tabs:start -->
6876

6977
### **Python3**
7078

7179
<!-- 这里可写当前语言的特殊实现逻辑 -->
7280

7381
```python
74-
82+
class Solution:
83+
def differenceOfSums(self, n: int, m: int) -> int:
84+
return sum(i if i % m else -i for i in range(1, n + 1))
7585
```
7686

7787
### **Java**
7888

7989
<!-- 这里可写当前语言的特殊实现逻辑 -->
8090

8191
```java
82-
92+
class Solution {
93+
public int differenceOfSums(int n, int m) {
94+
int ans = 0;
95+
for (int i = 1; i <= n; ++i) {
96+
ans += i % m == 0 ? -i : i;
97+
}
98+
return ans;
99+
}
100+
}
83101
```
84102

85103
### **C++**
86104

87105
```cpp
88-
106+
class Solution {
107+
public:
108+
int differenceOfSums(int n, int m) {
109+
int ans = 0;
110+
for (int i = 1; i <= n; ++i) {
111+
ans += i % m ? i : -i;
112+
}
113+
return ans;
114+
}
115+
};
89116
```
90117
91118
### **Go**
92119
93120
```go
121+
func differenceOfSums(n int, m int) (ans int) {
122+
for i := 1; i <= n; i++ {
123+
if i%m == 0 {
124+
ans -= i
125+
} else {
126+
ans += i
127+
}
128+
}
129+
return
130+
}
131+
```
132+
133+
### **TypeScript**
94134

135+
```ts
136+
function differenceOfSums(n: number, m: number): number {
137+
let ans = 0;
138+
for (let i = 1; i <= n; ++i) {
139+
ans += i % m ? i : -i;
140+
}
141+
return ans;
142+
}
95143
```
96144

97145
### **...**

solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README_EN.md

+51-3
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,78 @@ We return 0 - 15 = -15 as the answer.
5858

5959
## Solutions
6060

61+
**Solution 1: Simulation**
62+
63+
We traverse every number in the range $[1, n]$. If it is divisible by $m$, we subtract it from the answer. Otherwise, we add it to the answer.
64+
65+
After the traversal, we return the answer.
66+
67+
The time complexity is $O(n)$, where $n$ is the given integer. The space complexity is $O(1)$.
68+
6169
<!-- tabs:start -->
6270

6371
### **Python3**
6472

6573
```python
66-
74+
class Solution:
75+
def differenceOfSums(self, n: int, m: int) -> int:
76+
return sum(i if i % m else -i for i in range(1, n + 1))
6777
```
6878

6979
### **Java**
7080

7181
```java
72-
82+
class Solution {
83+
public int differenceOfSums(int n, int m) {
84+
int ans = 0;
85+
for (int i = 1; i <= n; ++i) {
86+
ans += i % m == 0 ? -i : i;
87+
}
88+
return ans;
89+
}
90+
}
7391
```
7492

7593
### **C++**
7694

7795
```cpp
78-
96+
class Solution {
97+
public:
98+
int differenceOfSums(int n, int m) {
99+
int ans = 0;
100+
for (int i = 1; i <= n; ++i) {
101+
ans += i % m ? i : -i;
102+
}
103+
return ans;
104+
}
105+
};
79106
```
80107
81108
### **Go**
82109
83110
```go
111+
func differenceOfSums(n int, m int) (ans int) {
112+
for i := 1; i <= n; i++ {
113+
if i%m == 0 {
114+
ans -= i
115+
} else {
116+
ans += i
117+
}
118+
}
119+
return
120+
}
121+
```
122+
123+
### **TypeScript**
84124

125+
```ts
126+
function differenceOfSums(n: number, m: number): number {
127+
let ans = 0;
128+
for (let i = 1; i <= n; ++i) {
129+
ans += i % m ? i : -i;
130+
}
131+
return ans;
132+
}
85133
```
86134

87135
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int differenceOfSums(int n, int m) {
4+
int ans = 0;
5+
for (int i = 1; i <= n; ++i) {
6+
ans += i % m ? i : -i;
7+
}
8+
return ans;
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func differenceOfSums(n int, m int) (ans int) {
2+
for i := 1; i <= n; i++ {
3+
if i%m == 0 {
4+
ans -= i
5+
} else {
6+
ans += i
7+
}
8+
}
9+
return
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int differenceOfSums(int n, int m) {
3+
int ans = 0;
4+
for (int i = 1; i <= n; ++i) {
5+
ans += i % m == 0 ? -i : i;
6+
}
7+
return ans;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def differenceOfSums(self, n: int, m: int) -> int:
3+
return sum(i if i % m else -i for i in range(1, n + 1))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function differenceOfSums(n: number, m: number): number {
2+
let ans = 0;
3+
for (let i = 1; i <= n; ++i) {
4+
ans += i % m ? i : -i;
5+
}
6+
return ans;
7+
}

solution/2800-2899/2895.Minimum Processing Time/README.md

+74-3
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,105 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:贪心 + 排序**
57+
58+
要使得处理完所有任务的时间最小,那么越早处于空闲状态的处理器应该处理耗时最长的 $4$ 个任务。
59+
60+
因此,我们对处理器的空闲时间和任务的耗时分别进行排序,然后依次将耗时最长的 $4$ 个任务分配给空闲时间最早的处理器,计算最大的结束时间即可。
61+
62+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为任务的数量。
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**
5967

6068
<!-- 这里可写当前语言的特殊实现逻辑 -->
6169

6270
```python
63-
71+
class Solution:
72+
def minProcessingTime(self, processorTime: List[int], tasks: List[int]) -> int:
73+
processorTime.sort()
74+
tasks.sort()
75+
ans = 0
76+
i = len(tasks) - 1
77+
for t in processorTime:
78+
ans = max(ans, t + tasks[i])
79+
i -= 4
80+
return ans
6481
```
6582

6683
### **Java**
6784

6885
<!-- 这里可写当前语言的特殊实现逻辑 -->
6986

7087
```java
71-
88+
class Solution {
89+
public int minProcessingTime(List<Integer> processorTime, List<Integer> tasks) {
90+
processorTime.sort((a, b) -> a - b);
91+
tasks.sort((a, b) -> a - b);
92+
int ans = 0, i = tasks.size() - 1;
93+
for (int t : processorTime) {
94+
ans = Math.max(ans, t + tasks.get(i));
95+
i -= 4;
96+
}
97+
return ans;
98+
}
99+
}
72100
```
73101

74102
### **C++**
75103

76104
```cpp
77-
105+
class Solution {
106+
public:
107+
int minProcessingTime(vector<int>& processorTime, vector<int>& tasks) {
108+
sort(processorTime.begin(), processorTime.end());
109+
sort(tasks.begin(), tasks.end());
110+
int ans = 0, i = tasks.size() - 1;
111+
for (int t : processorTime) {
112+
ans = max(ans, t + tasks[i]);
113+
i -= 4;
114+
}
115+
return ans;
116+
}
117+
};
78118
```
79119
80120
### **Go**
81121
82122
```go
123+
func minProcessingTime(processorTime []int, tasks []int) (ans int) {
124+
sort.Ints(processorTime)
125+
sort.Ints(tasks)
126+
i := len(tasks) - 1
127+
for _, t := range processorTime {
128+
ans = max(ans, t+tasks[i])
129+
i -= 4
130+
}
131+
return
132+
}
133+
134+
func max(a, b int) int {
135+
if a > b {
136+
return a
137+
}
138+
return b
139+
}
140+
```
83141

142+
### **TypeScript**
143+
144+
```ts
145+
function minProcessingTime(processorTime: number[], tasks: number[]): number {
146+
processorTime.sort((a, b) => a - b);
147+
tasks.sort((a, b) => a - b);
148+
let [ans, i] = [0, tasks.length - 1];
149+
for (const t of processorTime) {
150+
ans = Math.max(ans, t + tasks[i]);
151+
i -= 4;
152+
}
153+
return ans;
154+
}
84155
```
85156

86157
### **...**

0 commit comments

Comments
 (0)