-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinOperations.java
39 lines (31 loc) · 1.01 KB
/
MinOperations.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
class MinOperations {
public int minOperations(int[] nums, int xx) {
/*Runtime: 5 ms, faster than 95.08% of Java online submissions for Minimum Operations to Reduce X to Zero.
Memory Usage: 96.2 MB, less than 52.69% of Java online submissions for Minimum Operations to Reduce X to Zero.*/
int x=0;
for(int t:nums)
x+=t;
if(x==xx)
return nums.length;
x-=xx;
int l=0;
int sum=0;
int ans=-1;
// System.out.println(x);
for(int r=0; r<nums.length;r++)
{
sum+=nums[r];
// System.out.println(l+" "+r+" "+sum);
while(sum>=x && l<=r)
{
if(sum==x)
ans=Math.max(ans, r-l+1);
// System.out.println("in"+ ans);
sum-=nums[l++];
}
}
if(ans==-1)
return -1;
return nums.length-ans;
}
}