-
Notifications
You must be signed in to change notification settings - Fork 5
/
MinOpsToReduceXToZero.java
60 lines (53 loc) · 1.78 KB
/
MinOpsToReduceXToZero.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
55
56
57
58
59
60
/*https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/*/
/*DP solution*/
class Solution {
public int minOperations(int[] nums, int x) {
int[] prevTable = new int[1];
int[] currTable = new int[2];
int minOps = Integer.MAX_VALUE;
for (int len = nums.length-2; len >= 0; --len)
{
currTable = new int[prevTable.length+1];
int start = 0;
for (start = 0; start+len < nums.length-1; ++start )
{
int end = start+len;
currTable[start] = prevTable[start]+nums[end+1];
if (currTable[start] == x)
return nums.length-(end-start+1);
}
currTable[start] = prevTable[start-1]+nums[start-1];
if (currTable[start] == x)
return nums.length-(len+1);
prevTable = currTable;
}
for (int i = 0; i < nums.length; ++i)
if (currTable[i]+nums[i] == x)
return nums.length;
return -1;
}
}
/*Efficient solution using reverse logic*/
class Solution
{
public int minOperations(int[] nums, int x)
{
int totSum = 0;
for (int i = 0; i < nums.length; ++i)
totSum += nums[i];
int rem = totSum-x;
if (rem == 0) return nums.length;
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
int sum = 0;
int n = nums.length, len = 0;
map.put(0,-1);
for (int i = 0; i < n; ++i)
{
sum += nums[i];
map.put(sum,i);
if (map.containsKey(sum-rem))
len = Math.max(len,i-(Integer)map.get(sum-rem));
}
return len == 0 ? -1 : len == nums.length ? nums.length : n-len;
}
}