Skip to content

Commit 695f930

Browse files
committed
feat: add solutions to lc problem: No.1658
No.1658.Minimum Operations to Reduce X to Zero
1 parent 3980f8c commit 695f930

File tree

4 files changed

+132
-47
lines changed

4 files changed

+132
-47
lines changed

solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md

+45-16
Original file line numberDiff line numberDiff line change
@@ -323,30 +323,59 @@ impl Solution {
323323
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
324324
let n = nums.len();
325325
let target = nums.iter().sum::<i32>() - x;
326-
327-
328-
let (mut l, mut r) = (0, 0);
329-
let (mut sum, mut max) = (0, -1);
330-
while r < n {
331-
sum += nums[r];
332-
r += 1;
333-
while sum > target && l < r {
334-
sum -= nums[l];
335-
l += 1;
326+
if target < 0 {
327+
return -1;
328+
}
329+
let mut ans = i32::MAX;
330+
let mut sum = 0;
331+
let mut i = 0;
332+
for j in 0..n {
333+
sum += nums[j];
334+
while sum > target {
335+
sum -= nums[i];
336+
i += 1;
336337
}
337-
338-
339338
if sum == target {
340-
max = max.max((r - l) as i32);
339+
ans = ans.min((n - 1 - (j - i)) as i32)
341340
}
342341
}
342+
if ans == i32::MAX {
343+
return -1;
344+
}
345+
ans
346+
}
347+
}
348+
```
343349

350+
### **C**
344351

345-
if max == -1 {
346-
return max;
352+
```c
353+
#define min(a, b) (((a) < (b)) ? (a) : (b))
354+
355+
int minOperations(int *nums, int numsSize, int x) {
356+
int target = -x;
357+
for (int i = 0; i < numsSize; i++) {
358+
target += nums[i];
359+
}
360+
if (target < 0) {
361+
return -1;
362+
}
363+
int ans = INT_MAX;
364+
int sum = 0;
365+
int i = 0;
366+
for (int j = 0; j < numsSize; j++) {
367+
sum += nums[j];
368+
while (sum > target) {
369+
sum -= nums[i++];
370+
}
371+
if (sum == target) {
372+
ans = min(ans, numsSize - 1 - (j - i));
347373
}
348-
return n as i32 - max;
349374
}
375+
if (ans == INT_MAX) {
376+
return -1;
377+
}
378+
return ans;
350379
}
351380
```
352381

solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md

+45-16
Original file line numberDiff line numberDiff line change
@@ -291,30 +291,59 @@ impl Solution {
291291
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
292292
let n = nums.len();
293293
let target = nums.iter().sum::<i32>() - x;
294-
295-
296-
let (mut l, mut r) = (0, 0);
297-
let (mut sum, mut max) = (0, -1);
298-
while r < n {
299-
sum += nums[r];
300-
r += 1;
301-
while sum > target && l < r {
302-
sum -= nums[l];
303-
l += 1;
294+
if target < 0 {
295+
return -1;
296+
}
297+
let mut ans = i32::MAX;
298+
let mut sum = 0;
299+
let mut i = 0;
300+
for j in 0..n {
301+
sum += nums[j];
302+
while sum > target {
303+
sum -= nums[i];
304+
i += 1;
304305
}
305-
306-
307306
if sum == target {
308-
max = max.max((r - l) as i32);
307+
ans = ans.min((n - 1 - (j - i)) as i32)
309308
}
310309
}
310+
if ans == i32::MAX {
311+
return -1;
312+
}
313+
ans
314+
}
315+
}
316+
```
311317

318+
### **C**
312319

313-
if max == -1 {
314-
return max;
320+
```c
321+
#define min(a, b) (((a) < (b)) ? (a) : (b))
322+
323+
int minOperations(int *nums, int numsSize, int x) {
324+
int target = -x;
325+
for (int i = 0; i < numsSize; i++) {
326+
target += nums[i];
327+
}
328+
if (target < 0) {
329+
return -1;
330+
}
331+
int ans = INT_MAX;
332+
int sum = 0;
333+
int i = 0;
334+
for (int j = 0; j < numsSize; j++) {
335+
sum += nums[j];
336+
while (sum > target) {
337+
sum -= nums[i++];
338+
}
339+
if (sum == target) {
340+
ans = min(ans, numsSize - 1 - (j - i));
315341
}
316-
return n as i32 - max;
317342
}
343+
if (ans == INT_MAX) {
344+
return -1;
345+
}
346+
return ans;
318347
}
319348
```
320349
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#define min(a, b) (((a) < (b)) ? (a) : (b))
2+
3+
int minOperations(int *nums, int numsSize, int x) {
4+
int target = -x;
5+
for (int i = 0; i < numsSize; i++) {
6+
target += nums[i];
7+
}
8+
if (target < 0) {
9+
return -1;
10+
}
11+
int ans = INT_MAX;
12+
int sum = 0;
13+
int i = 0;
14+
for (int j = 0; j < numsSize; j++) {
15+
sum += nums[j];
16+
while (sum > target) {
17+
sum -= nums[i++];
18+
}
19+
if (sum == target) {
20+
ans = min(ans, numsSize - 1 - (j - i));
21+
}
22+
}
23+
if (ans == INT_MAX) {
24+
return -1;
25+
}
26+
return ans;
27+
}

solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ impl Solution {
22
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
33
let n = nums.len();
44
let target = nums.iter().sum::<i32>() - x;
5-
6-
let (mut l, mut r) = (0, 0);
7-
let (mut sum, mut max) = (0, -1);
8-
while r < n {
9-
sum += nums[r];
10-
r += 1;
11-
while sum > target && l < r {
12-
sum -= nums[l];
13-
l += 1;
5+
if target < 0 {
6+
return -1;
7+
}
8+
let mut ans = i32::MAX;
9+
let mut sum = 0;
10+
let mut i = 0;
11+
for j in 0..n {
12+
sum += nums[j];
13+
while sum > target {
14+
sum -= nums[i];
15+
i += 1;
1416
}
15-
1617
if sum == target {
17-
max = max.max((r - l) as i32);
18+
ans = ans.min((n - 1 - (j - i)) as i32)
1819
}
1920
}
20-
21-
if max == -1 {
22-
return max;
21+
if ans == i32::MAX {
22+
return -1;
2323
}
24-
return n as i32 - max;
24+
ans
2525
}
2626
}

0 commit comments

Comments
 (0)