New to Dynamic Programming.
I have pseudo-code for an algorithm that takes in a list of non-negative integers and the desired sum, then outputs true if the given sum can be made from the given integers (no integers can be used more than once), and false if not. I will post my pseudo-code below. This is how I intuitively solved the problem, and I don't believe it follows dynamic programming concepts.
So my question is: Is there a neat trick for refactoring existing code to follow a more Dynamic Programming approach?
Pseudo-code:
1. Make a 2D boolean array "arr" of size n + 1 row and sum + 1 columns and all cells will be false initially.
2. The value of "arr[i][j]" will be true if there is a subset of list[0..j-1] with a sum equal to i
3. Assign all the values of first column to true subset[i][0] = true because if target is 0 then it is also possible
4. Assign false to first row "subset[0][i] = false" bacause if sum is not 0 and list is empty, then answer is false
5. Run a for loop from i = 1 to i <= n and for each i do the following
// Fill the subset table in botton up manner
a) Run a for loop from j = 1 to j <= sum and for each j do the following:
i) if (j < list[i - 1]) then assign arr[i][j] = arr[i - 1][j];
ii) if (j >= list[i - 1]) or arr[i - 1][j - list[i - 1]] then assign arr[i][j] = arr[i - 1][j]
6. Now if the value of arr[n][sum] is true that means we have a set which sums upto to given target and if it has false then we do not have