7.1. 1-3 sub set sums
- Dimensions: The table is two dimensional and has a size of
- Subproblems: the entry
dp[i][j]is a boolean value that determines whether is a 1-3 subset sum of - Recursion:
- Base Case:
dp[0][0]is true and all other values are false - Normal Case: the entry
dp[i][j]is true ifdp[i-1][j],d[i-1][j-A[i]]ord[i-1][j-3*A[i]]is true.
- Base Case:
- Justification: Correct because the last decision for item
iis exactly one of:
- don’t take
- take
1·A[i] - take
3·A[i] - and the remainder depends on the first
i-1items. Since we assume we have already calculated all the cases withi-1, we get whetherdp[i][j]is achievable. - Calculation order: Increase
ifrom1..n. For eachi, increasejfrom0..b. Each state only depends on rowi-1, already computed. - Extracting the solution: take the value in
dp[n][b] - Running time:
7.2. Longest ascending subsequence
a)
dp[i]=max(dp[j] for all j=1...n and A[j]<=A[i])+1
dp[1]=1
b)
DP-table
i | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
dp[i] | 1 | 2 | 1 | 1 | 3 | 2 | 3 | 4 | 4 |
| Answer: |
7.4 String counting
- Dimensions: The table is three dimensional and has a size of
- Subproblems: the entry
dp[i][j][l]means the number of binary strings with following properties:- has length
i - has
k“11” in the string - the last bit is
l(0 or 1)
- has length
- Recursion:
- Base Case:
dp[1][0][0]=1dp[1][0][1]=12. Normal Case:dp[i][j][0]=dp[i-1][j][0]+dp[i-1][j][1]dp[i][j][1]=dp[i-1][j][0]+dp[i-1][j-1][1]- Calculation order:
for i in 1..n:
for j in 0..k:
dp[i][j][0]=dp[i-1][j][0]+dp[i-1][j][1]
if j>0:
dp[i][j][1]=dp[i-1][j][0]+dp[i-1][j-1][1]
else:
dp[i][j][1]=dp[i-1][j][0]
- Extracting the solution: take the value of
dp[n][k][0]+dp[n][k][1] - Running time: