File tree 4 files changed +82
-0
lines changed
2894.Divisible and Non-divisible Sums Difference
2896.Apply Operations to Make Two Strings Equal
4 files changed +82
-0
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,17 @@ class Solution {
100
100
}
101
101
```
102
102
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
+
103
114
### ** C++**
104
115
105
116
``` cpp
Original file line number Diff line number Diff line change @@ -90,6 +90,17 @@ class Solution {
90
90
}
91
91
```
92
92
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
+
93
104
### ** C++**
94
105
95
106
``` cpp
Original file line number Diff line number Diff line change @@ -144,6 +144,36 @@ class Solution {
144
144
}
145
145
```
146
146
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
+
147
177
### ** C++**
148
178
149
179
``` cpp
Original file line number Diff line number Diff line change @@ -134,6 +134,36 @@ class Solution {
134
134
}
135
135
```
136
136
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
+
137
167
### ** C++**
138
168
139
169
``` cpp
You can’t perform that action at this time.
0 commit comments