LeetCode-in-All

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

class Solution {
    func findTargetSumWays(_ nums: [Int], _ target: Int) -> Int {
        let sum = nums.reduce(0,+)
        let positiveSubsetVal = (target + sum)/2
        if target > sum || target < -sum {
            return 0
        } else if positiveSubsetVal - (sum - positiveSubsetVal) != target {
            return 0
        }
        var prev = Array(repeating: 0, count: positiveSubsetVal+1)
        var curr = Array(repeating: 0, count: positiveSubsetVal+1)
        prev[0] = 1
        for row in 0..<nums.count {
            for col in 0..<positiveSubsetVal+1 {
                let includingCount = col - nums[row] >= 0 ? prev[col - nums[row]] : 0
                curr[col] = prev[col] + includingCount
            }
            prev = curr
        }
        
        return curr[curr.count-1]
    }
}