-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathTargetSum.java
More file actions
38 lines (27 loc) · 993 Bytes
/
Copy pathTargetSum.java
File metadata and controls
38 lines (27 loc) · 993 Bytes
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
package leetcode.medium;
import java.util.HashMap;
import java.util.Map;
public class TargetSum {
int findTargetSumWays(int[] nums, int target) {
// Base case: we start with the sum 0 and one way to achieve it.
Map<Integer, Integer> dp = new HashMap<>();
dp.put(0, 1);
for (int num : nums) {
// Temporary map to store new dp states for the current iteration
Map<Integer, Integer> nextDp = new HashMap<>();
// For every possible sum in dp, add and subtract the current number
for (int sum : dp.keySet()) {
int count = dp.get(sum);
// Add current number to sum
nextDp.put(sum + num,
nextDp.getOrDefault(sum + num, 0) + count);
// Subtract current number from sum
nextDp.put(sum - num,
nextDp.getOrDefault(sum - num, 0) + count);
}
// Move to the next iteration by updating dp with the new states
dp = nextDp;
}
return dp.getOrDefault(target, 0);
}
}