|
| 1 | +/** |
| 2 | + * You are given a list of non-negative integers, a1, a2, ..., an, and a |
| 3 | + * target, S. Now you have 2 symbols + and -. For each integer, you should |
| 4 | + * choose one from + and - as its new symbol. |
| 5 | + * |
| 6 | + * Find out how many ways to assign symbols to make sum of integers equal to |
| 7 | + * target S. |
| 8 | + * |
| 9 | + * Example 1: |
| 10 | + * Input: nums is [1, 1, 1, 1, 1], S is 3. |
| 11 | + * Output: 5 |
| 12 | + * |
| 13 | + * Explanation: |
| 14 | + * -1+1+1+1+1 = 3 |
| 15 | + * +1-1+1+1+1 = 3 |
| 16 | + * +1+1-1+1+1 = 3 |
| 17 | + * +1+1+1-1+1 = 3 |
| 18 | + * +1+1+1+1-1 = 3 |
| 19 | + * |
| 20 | + * There are 5 ways to assign symbols to make the sum of nums be target 3. |
| 21 | + * |
| 22 | + * Note: |
| 23 | + * The length of the given array is positive and will not exceed 20. |
| 24 | + * The sum of elements in the given array will not exceed 1000. |
| 25 | + * Your output answer is guaranteed to be fitted in a 32-bit integer. |
| 26 | + */ |
| 27 | + |
| 28 | +public class TargetSum494 { |
| 29 | + public int findTargetSumWays(int[] nums, int S) { |
| 30 | + if (S > 1000 || S < -1000) return 0; |
| 31 | + int N = nums.length; |
| 32 | + int[][] dp = new int[N+1][2001]; |
| 33 | + |
| 34 | + dp[0][1000] = 1; |
| 35 | + for (int i=1; i<=N; i++) { |
| 36 | + int n = nums[i-1]; |
| 37 | + for (int j=0; j<2001; j++) { |
| 38 | + dp[i][j] = (j+n < 2001 ? dp[i-1][j+n] : 0) + (j-n >=0 ? dp[i-1][j-n] : 0); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return dp[N][S + 1000]; |
| 43 | + } |
| 44 | + |
| 45 | + public int findTargetSumWays2(int[] nums, int S) { |
| 46 | + if (S > 1000 || S < -1000) return 0; |
| 47 | + int N = nums.length; |
| 48 | + int[] dp = new int[2001]; |
| 49 | + |
| 50 | + dp[1000] = 1; |
| 51 | + for (int i=1; i<=N; i++) { |
| 52 | + int n = nums[i-1]; |
| 53 | + int[] tmp = new int[2001]; |
| 54 | + for (int j=0; j<2001; j++) tmp[j] = dp[j]; |
| 55 | + for (int j=0; j<2001; j++) { |
| 56 | + dp[j] = (j+n < 2001 ? tmp[j+n] : 0) + (j-n >=0 ? tmp[j-n] : 0); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return dp[S + 1000]; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * https://leetcode.com/problems/target-sum/solution/ |
| 66 | + */ |
| 67 | + public int findTargetSumWays3(int[] nums, int S) { |
| 68 | + int[][] dp = new int[nums.length][2001]; |
| 69 | + dp[0][nums[0] + 1000] = 1; |
| 70 | + dp[0][-nums[0] + 1000] += 1; |
| 71 | + for (int i = 1; i < nums.length; i++) { |
| 72 | + for (int sum = -1000; sum <= 1000; sum++) { |
| 73 | + if (dp[i - 1][sum + 1000] > 0) { |
| 74 | + dp[i][sum + nums[i] + 1000] += dp[i - 1][sum + 1000]; |
| 75 | + dp[i][sum - nums[i] + 1000] += dp[i - 1][sum + 1000]; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return S > 1000 ? 0 : dp[nums.length - 1][S + 1000]; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + /** |
| 84 | + * https://leetcode.com/problems/target-sum/solution/ |
| 85 | + */ |
| 86 | + public int findTargetSumWays4(int[] nums, int S) { |
| 87 | + int[] dp = new int[2001]; |
| 88 | + dp[nums[0] + 1000] = 1; |
| 89 | + dp[-nums[0] + 1000] += 1; |
| 90 | + for (int i = 1; i < nums.length; i++) { |
| 91 | + int[] next = new int[2001]; |
| 92 | + for (int sum = -1000; sum <= 1000; sum++) { |
| 93 | + if (dp[sum + 1000] > 0) { |
| 94 | + next[sum + nums[i] + 1000] += dp[sum + 1000]; |
| 95 | + next[sum - nums[i] + 1000] += dp[sum + 1000]; |
| 96 | + } |
| 97 | + } |
| 98 | + dp = next; |
| 99 | + } |
| 100 | + return S > 1000 ? 0 : dp[S + 1000]; |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments