Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
This is the wrong solution to the problem. The right solution is below,public in...

Avinash Agarwal

Apr 17, 2022

This is the wrong solution to the problem. The right solution is below,

public int findTargetSumWays(int[] nums, int S) { int total = Arrays.stream(nums).sum(); int[] dp = new int[2 * total + 1]; dp[nums[0] + total] = 1; dp[-nums[0] + total] += 1;

for (int i = 1; i < nums.length; i++) { int[] next = new int[2 * total + 1]; for (int sum = -total; sum 0) { next[sum + nums[i] + total] += dp[sum + total]; next[sum - nums[i] + total] += dp[sum + total]; } } dp = next; }

return Math.abs(S) > total ? 0 : dp[S + total]; }

1

0

Comments
Comments
Design Gurus
Design Gurus3 years ago

What is wrong with our solution?

On this page