Skip to content

Commit f7b7d37

Browse files
add Java solution for 1658
1 parent c44bf48 commit f7b7d37

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ _If you like this project, please leave me a star._ ★
3232
|1664|[Ways to Make a Fair Array](https://leetcode.com/problems/ways-to-make-a-fair-array/)|[Javascript](./javascript/_1664.js) ||Medium|Greedy|
3333
|1663|[Smallest String With A Given Numeric Value](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1663.java) ||Medium|Greedy|
3434
|1662|[Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1662.java) ||Easy|String|
35-
|1658|[Minimum Operations to Reduce X to Zero](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)|[Javascript](./javascript/_1658.js)||Medium|Greedy|
35+
|1658|[Minimum Operations to Reduce X to Zero](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)|[Javascript](./javascript/_1658.js) | [Java](../master/src/main/java/com/fishercoder/solutions/_1658.java)||Medium|Greedy|
3636
|1657|[Determine if Two Strings Are Close](https://leetcode.com/problems/determine-if-two-strings-are-close/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1657.java) |[:tv:](https://youtu.be/-jXQK-UeChU)|Medium|Greedy|
3737
|1656|[Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1656.java) ||Easy|Array, Design|
3838
|1652|[Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1652.java) ||Easy|Array|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1658 {
4+
public static class Solution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/936074/JavaPython-3-Sliding-window%3A-Longest-subarray-sum-to-the-target-sum(nums)-x.
7+
*/
8+
public int minOperations(int[] nums, int x) {
9+
int sum = 0;
10+
for (int n : nums) {
11+
sum += n;
12+
}
13+
int target = sum - x;
14+
int len = nums.length;
15+
int size = Integer.MIN_VALUE;
16+
for (int left = -1, right = 0, windowSum = 0; right < len; right++) {
17+
windowSum += nums[right];
18+
while (left + 1 < len && windowSum > target) {
19+
left++;
20+
windowSum -= nums[left];
21+
}
22+
if (windowSum == target) {
23+
size = Math.max(size, right - left);
24+
}
25+
}
26+
return size < 0 ? -1 : len - size;
27+
}
28+
29+
}
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1658;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1658Test {
10+
private static _1658.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1658.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.minOperations(new int[]{1, 1, 4, 2, 3}, 5));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(-1, solution1.minOperations(new int[]{5, 6, 7, 8, 9}, 4));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(5, solution1.minOperations(new int[]{3, 2, 20, 1, 1, 3}, 10));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(16, solution1.minOperations(new int[]{8828, 9581, 49, 9818, 9974, 9869, 9991, 10000, 10000, 10000, 9999, 9993, 9904, 8819, 1231, 6309}, 134365));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)