<p>You are given an integer array <code>nums</code> and an integer <code>target</code>.</p>

<p>You want to build an <strong>expression</strong> out of nums by adding one of the symbols <code>&#39;+&#39;</code> and <code>&#39;-&#39;</code> before each integer in nums and then concatenate all the integers.</p>

<ul>
	<li>For example, if <code>nums = [2, 1]</code>, you can add a <code>&#39;+&#39;</code> before <code>2</code> and a <code>&#39;-&#39;</code> before <code>1</code> and concatenate them to build the expression <code>&quot;+2-1&quot;</code>.</li>
</ul>

<p>Return the number of different <strong>expressions</strong> that you can build, which evaluates to <code>target</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,1,1,1], target = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> 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
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1], target = 1
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 20</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
	<li><code>0 &lt;= sum(nums[i]) &lt;= 1000</code></li>
	<li><code>-1000 &lt;= target &lt;= 1000</code></li>
</ul>

class Solution { // DP-22
private:
    int memo(int ind, int target, vector<int>& nums, vector<vector<int>>& dp) {
        if (ind == 0) {
            if (target == 0 && nums[0] == 0)    return 2;
            if (nums[0] == target || target == 0)  return 1;
            return 0;
        }
        if (dp[ind][target] != -1)  return dp[ind][target];

        int notTaken = memo(ind-1, target, nums, dp);
        int taken = 0;
        if (nums[ind] <= target)
            taken = memo(ind-1, target-nums[ind], nums, dp);

        return dp[ind][target] = taken + notTaken;
    }

    int tabulation(vector<int>& nums, int tar) {
        int n = nums.size();
        vector<vector<int>> dp(n, vector<int>(tar+1, 0));
        if (nums[0] == 0)   
            dp[0][0] = 2;
        else
            dp[0][0] = 1;

        if (nums[0] != 0 && nums[0] <= tar)
            dp[0][nums[0]] = 1;

        for (int ind = 1; ind<n; ind++) {
            for (int target = 0; target <= tar; target++) {
                int notTaken = dp[ind-1][target];
                int taken = 0;
                if (nums[ind] <= target)
                    taken = dp[ind-1][target-nums[ind]];
                dp[ind][target] = taken + notTaken;
            }
        }

        return dp[n-1][tar];
    }

    int spaceOptimized(vector<int>& nums, int tar) {
        int n = nums.size();
        vector<int> prev(tar+1, 0);
        if (nums[0] == 0)
            prev[0] = 2;
        else 
            prev[0] = 1;
        
        if (nums[0] != 0 && nums[0] <= tar)
            prev[nums[0]] = 1;

        for (int ind = 1; ind <n; ind++) {
            vector<int> curr(tar+1, 0);
            for (int target = 0; target<=tar; target++) {
                int notTaken = prev[target];
                int taken = 0;
                if (nums[ind] <= target)
                    taken = prev[target-nums[ind]];
                curr[target] = taken + notTaken;
            }
            prev = curr;
        }

        return prev[tar];
    }

public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int totSum = 0, n = nums.size();
        for (int i=0; i<n; i++)
            totSum += nums[i];

        if (totSum < target)    
            return 0;
        if ((totSum - target) % 2)
            return 0;

        int s2 = (totSum - target) / 2;

        // Memorization
        /*
        vector<vector<int>> dp(n, vector<int>(s2+1, -1));
        return memo(n-1, s2, nums, dp);
        */

        // Tabulation
        // return tabulation(nums, s2);

        //Space Optimization
        return spaceOptimized(nums, s2);
    }
};
14091