-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimumOperationsToReduceXToZero.java
54 lines (50 loc) · 1.43 KB
/
MinimumOperationsToReduceXToZero.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/
// #two-pointer #binary-search #sliding-window #greedy
class Solution {
public int minOperations(int[] nums, int x) {
// count total from left and put to hashmap, if total > x, stop;
// run from right, calculate current total, and find y that current total + y = x in hashmap
// no need to check
if (nums[0] == x) return 1;
if (nums[nums.length - 1] == x) return 1;
int total = 0;
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length - 1; i++) {
if (total + nums[i] <= x) {
total += nums[i];
map.put(total, i + 1);
} else {
break;
}
}
int result = 100001;
int current_total = 0;
for (int i = nums.length - 1; i > 0; i--) {
current_total += nums[i];
int count = nums.length - i;
if (current_total == x) {
if (count < result) {
result = count;
break;
}
} else if (current_total < x) {
int gap = x - current_total;
if (map.containsKey(gap)) {
count += map.get(gap);
if (count < result) {
result = count;
}
}
} else {
if (map.containsKey(x)) {
count = map.get(x);
if (count < result) {
result = count;
}
}
break;
}
}
return (result < 100001) ? result : -1;
}
}