Skip to content

Commit e1a0ba6

Browse files
authored
feat: add new solutions to lc problems: No.2894 2896 (#1783)
1 parent b88b4e0 commit e1a0ba6

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ class Solution {
100100
}
101101
```
102102

103+
```java
104+
class Solution {
105+
public int differenceOfSums(int n, int m) {
106+
int sum = n * (n + 1) / 2;
107+
int k = n / m;
108+
int nums2 = k * (k + 1) / 2 * m;
109+
return sum - nums2 * 2;
110+
}
111+
}
112+
```
113+
103114
### **C++**
104115

105116
```cpp

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

+11
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ class Solution {
9090
}
9191
```
9292

93+
```java
94+
class Solution {
95+
public int differenceOfSums(int n, int m) {
96+
int sum = n * (n + 1) / 2;
97+
int k = n / m;
98+
int nums2 = k * (k + 1) / 2 * m;
99+
return sum - nums2 * 2;
100+
}
101+
}
102+
```
103+
93104
### **C++**
94105

95106
```cpp

solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,36 @@ class Solution {
144144
}
145145
```
146146

147+
```java
148+
class Solution {
149+
public int minOperations(String s1, String s2, int x) {
150+
int n = s1.length();
151+
int inf = 50_000;
152+
int one = inf, two = inf, last = inf;
153+
int done = 0;
154+
for (int i = 0; i < n; i++) {
155+
if (s1.charAt(i) == s2.charAt(i)) {
156+
one = Math.min(one, last);
157+
last = last + 1;
158+
two = two + 1;
159+
continue;
160+
}
161+
if (done < n) {
162+
one = Math.min(two + 1, done + x);
163+
last = Math.min(two + x, done);
164+
done = two = inf;
165+
continue;
166+
}
167+
done = Math.min(one + x, last + 1);
168+
two = one;
169+
one = last = inf;
170+
continue;
171+
}
172+
return done == inf ? -1 : done;
173+
}
174+
}
175+
```
176+
147177
### **C++**
148178

149179
```cpp

solution/2800-2899/2896.Apply Operations to Make Two Strings Equal/README_EN.md

+30
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,36 @@ class Solution {
134134
}
135135
```
136136

137+
```java
138+
class Solution {
139+
public int minOperations(String s1, String s2, int x) {
140+
int n = s1.length();
141+
int inf = 50_000;
142+
int one = inf, two = inf, last = inf;
143+
int done = 0;
144+
for (int i = 0; i < n; i++) {
145+
if (s1.charAt(i) == s2.charAt(i)) {
146+
one = Math.min(one, last);
147+
last = last + 1;
148+
two = two + 1;
149+
continue;
150+
}
151+
if (done < n) {
152+
one = Math.min(two + 1, done + x);
153+
last = Math.min(two + x, done);
154+
done = two = inf;
155+
continue;
156+
}
157+
done = Math.min(one + x, last + 1);
158+
two = one;
159+
one = last = inf;
160+
continue;
161+
}
162+
return done == inf ? -1 : done;
163+
}
164+
}
165+
```
166+
137167
### **C++**
138168

139169
```cpp

0 commit comments

Comments
 (0)