MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / calculate

Method calculate

java/0494-target-sum.java:11–24  ·  view source on GitHub ↗
(int[] nums, int i, int sum, int S)

Source from the content-addressed store, hash-verified

9 }
10
11 public int calculate(int[] nums, int i, int sum, int S) {
12 String s = i + "," + sum;
13
14 if (i == nums.length) {
15 return (sum == S)? 1 : 0;
16 }
17 if(dp.containsKey(s)){
18 return dp.get(s);
19 }
20
21 int res = calculate(nums, i + 1, sum + nums[i], S) + calculate(nums, i + 1, sum - nums[i], S);
22 dp.put(s, res);
23 return res;
24 }
25}
26
27/* Alternative Better Complexity Solution

Callers 1

findTargetSumWaysMethod · 0.95

Calls 2

getMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected