LeetCode-in-Ruby.github.io

494. Target Sum

Medium

You are given an integer array nums and an integer target.

You want to build an expression out of nums by adding one of the symbols '+' and '-' before each integer in nums and then concatenate all the integers.

Return the number of different expressions that you can build, which evaluates to target.

Example 1:

Input: nums = [1,1,1,1,1], target = 3

Output: 5

Explanation:

There are 5 ways to assign symbols to make the sum of nums be target 3.
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3 

Example 2:

Input: nums = [1], target = 1

Output: 1

Constraints:

Solution

# @param {Integer[]} nums
# @param {Integer} target
# @return {Integer}
def find_target_sum_ways(nums, s)
  sum = nums.sum
  s = s.abs

  # Invalid s, just return 0
  return 0 if s > sum || (sum + s).odd?

  dp = Array.new((sum + s) / 2 + 1) {Array.new(nums.length + 1, 0)}
  dp[0][0] = 1

  # empty knapsack must be processed specially
  nums.each_with_index do |num, i|
    dp[0][i + 1] = num.zero? ? dp[0][i] * 2 : dp[0][i]
  end

  (1...dp.length).each do |i|
    (0...nums.length).each do |j|
      dp[i][j + 1] += dp[i][j]
      dp[i][j + 1] += dp[i - nums[j]][j] if nums[j] <= i
    end
  end

  dp[(sum + s) / 2][nums.length]
end