Dynamic Programming Series

Dynamic Programming Series

Day 4

The Target Sum

 

Problem Description

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make the sum of integers equal to target S.

Let's Take an Example:

[1,1,1,1,1] and Target Sum = 3

+1+1+1+1+1 = 5

-1+1+1+1+1 = 3

Count = 1

Problem Description

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make the sum of integers equal to target S.

Let's Take an Example:

[1,1,1,1,1] and Target Sum = 3

+1+1+1+1+1 = 5

+1-1+1+1+1 = 3

Count = 2

Problem Description

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make the sum of integers equal to target S.

Let's Take an Example:

[1,1,1,1,1] and Target Sum = 3

+1+1+1+1+1 = 5

+1+1-1+1+1 = 3

 

Count = 3

Problem Description

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make the sum of integers equal to target S.

Let's Take an Example:

[1,1,1,1,1] and Target Sum = 3

+1+1+1+1+1 = 5

+1+1+1-1+1 = 3

 
 

Count = 4

Problem Description

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make the sum of integers equal to target S.

Let's Take an Example:

[1,1,1,1,1] and Target Sum = 3

+1+1+1+1+1 = 5

+1+1+1+1-1 = 3

 
 
 

Count = 5

Now start thinking of it in a naive approach.

 

0

1 1 1 1 1

Add first number

Subtract First number

+1

-1

i = 0
1 1 1 1 1

0

Add first number

Subtract First number

-1

Add second number

Subtract Second number

0

-2

0

0

0

+1

Add second number

Subtract Second number

+2

i = 1
i = 0
1 1 1 1 1

0

Add first number

Subtract First number

-1

Add second number

Subtract Second number

-2

0

+1

Add second number

Subtract Second number

+2

0

Add third number

Subtract third number

+3

+1

Add third number

Subtract third number

+1

0

-1

-1

Add third number

Subtract third number

0

-1

-1

+1

0

Add third number

Subtract third number

0

-1

-1

+1

Add third number

Subtract third number

0

-1

-1

Subtract third number

0

-1

-3

-1

i = 1
i = 2
i = 0

Let's write the code for this approach

int count = 0;
void countWays(vector<int>& nums, int S, int t, int i)
{
    if (i == nums.size())
    {
        if (t == S)
            count++;
    }  
    else
    {
        countWays(nums, S, t-nums[i], i+1);
        countWays(nums, S, t+nums[i], i+1);    
    }
}

n+1 level where n = number of elements in array

2^n
2^0
2^1
2^n

level 0 :     = 1 node

level 1:     = 2 nodes

Total  Complexity of this approach is 

Each level contains     nodes i.e

Here Comes the Saviour the DYNAMIC PROGRAMMING

Before Moving to the DP solution let's understand basic intuition exponential recursion to Dynamic Programming

exponential recursion to Dynamic Programming

We will introduce a concept of states here in Dynamic Programming

At each ith level in our recursion tree, we are getting the same sum more than one times thus producing the redundant calls.

0

Add first number

Subtract First number

+1

-1

0

1 1 1 1 1

Add first number

Subtract First number

-1

Add second number

Subtract Second number

0

-2

0

0

0

+1

Add second number

Subtract Second number

+2

0

1 1 1 1 1

Add first number

Subtract First number

-1

Add second number

Subtract Second number

-2

0

+1

Add second number

Subtract Second number

+2

0

Add third number

Subtract third number

+3

+1

Add third number

Subtract third number

+1

0

-1

-1

Add third number

Subtract third number

0

-1

-1

+1

0

Add third number

Subtract third number

0

-1

-1

+1

Add third number

Subtract third number

0

-1

-1

Subtract third number

0

-1

-3

-1

1 1 1 1 1

Add first number

Subtract First number

0

+1

-1

Add second number

Subtract Second number

+2

0

Add second number

Subtract Second number

0

-2

Add third number

Subtract third number

+3

0

+1

Add third number

Subtract third number

+1

0

-1

Add third number

Subtract third number

-1

0

+1

-3

2 times
3 times
3 times

We can see that we can create our memoized container with states (i, sum) which represent the count of occurrence of sum until that i

dp[i][sum] = count\ of\ occurence\ of\ sum\ till\ index\ i

A state in Dynamic Programming is defined by a number of necessary variables at a particular instant that are required to calculate the optimal result.

In this case i,sum.

Now we Can start Coding our DP solution

int findTargetSumWays(vector<int>& nums, int S) {
    if (S > 1000)
        return 0;
    int limitSum = 2001;
    int origin = 1000;
    int n = nums.size();
    vector <vector<int>> dp(n+1, vector<int>(limitSum, 0));
    dp[0][0+origin] = 1;
    for (int i = 1;i <= n;i++)
    {
        for (int t = 0;t < limitSum;t++)
        {
            if (dp[i-1][t] > 0)
            {
                dp[i][t+nums[i-1]] += dp[i-1][t];
                dp[i][t-nums[i-1]] += dp[i-1][t];
            }
        }
    }
    return dp[n][S+origin];
}
0 0 0 0 0 1 0 0 0 0

-5

-4

-3

-2

-1

0

1

2

3

4

0

1

2

3

4

5

6

7

8

9

0

5

10

0 0 0 0 0 1 0 0 0 0

-5

-4

-3

-2

-1

0

1

2

3

4

0

1

2

3

4

5

6

7

8

9

0

5

10

0 0 0 0 1 0 1 0 0 0

-5

-4

-3

-2

-1

0

1

2

3

4

0

1

2

3

4

5

6

7

8

9

0

5

10

-nums[0]
+nums[0]

Sums

Mapping

Sums

Mapping

0 0 0 0 0 1 0 0 0 0

-5

-4

-3

-2

-1

0

1

2

3

4

0

1

2

3

4

5

6

7

8

9

0

5

10

0 0 0 0 1 0 1 0 0 0

-5

-4

-3

-2

-1

0

1

2

3

4

0

1

2

3

4

5

6

7

8

9

0

5

10

0 0 0 1 0 2 0 1 0 0

-5

-4

-3

-2

-1

0

1

2

3

4

0

1

2

3

4

5

6

7

8

9

0

5

10

-nums[1]
+nums[1]
-nums[1]
+nums[1]
-nums[0]
+nums[0]

Sums

Mapping

Sums

Mapping

Sums

Mapping

Time Complexity of this Solution.

O(n*sums) \newline here\ sums = range\ of\ sums\ which\ is\ from\ -1000\ to\ 1000 \newline n = size\ of\ array

So that's It for Day 4

So that's It for Day 4

Concepts that we have learnt

So that's It for Day 1 

We have taken a very first step in Dynamic Programming.

Concepts that we have learnt

1. Overlapping Subproblems in DP

So that's It for Day 1 

We have taken a very first step in Dynamic Programming.

Concepts that we have learnt

1. Overlapping Subproblems in DP

2. What are the States in DP

So that's It for Day 1 

We have taken a very first step in Dynamic Programming.

Concepts that we have learnt

1. Overlapping Subproblems in DP

2. What are the States in DP

3. Representation of 2-dimensional matrix in DP states

So that's It for Day 1 

We have taken a very first step in Dynamic Programming.

Concepts that we have learnt

1. Overlapping Subproblems in DP

2. What are States in DP.

3. Representation of 2-dimensional matrix in DP states

Still, Have any Doubts???

Still, Have any Doubts???

Comment it You will get a reply

OR

Send your doubt to email provided in description.

Made with Slides.com