Tuf M
Tuf M
Colour Representation
Capacity
1 1 0 0
6 2 1 0
10 3 2 0
16 5 3 0
Capacity = 0
Max profit for every subarray = 0
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0
10 3 2 0
16 5 3 0
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1
10 3 2 0
16 5 3 0
Capacity = 1, Index =1
since item at index '1' has weight '2', which is greater than the capacity
'1', so we will take the dp[index-1] [capacity]
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6
10 3 2 0
16 5 3 0
Capacity = 2. Index = 1
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[1] + dp[1- 1][2-weight[1]] >dp[1-1][2]
6+0>1
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6 7
10 3 2 0
16 5 3 0
Capacity = 3. Index = 1
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[1] + dp[1- 1][3-weight[1]] >dp[1-1][3]
6+1>1
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6 7 7 7 7 7
10 3 2 0
16 5 3 0
Capacity = 4 - 7, Index = 1
Max weight till [index 1] = 3
[ ind 4- find 7 ] Profit will be same as of capacity=2, index=1
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6 7 7 7 7 7
10 3 2 0 1 6
16 5 3 0
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6 7 7 7 7 7
10 3 2 0 1 6 10
16 5 3 0
Capacity = 3. Index = 2
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[2] + dp[2- 1][3-weight[1]] >dp[2-1][3]
10 + 0 > 7
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6 7 7 7 7 7
10 3 2 0 1 6 10 11
16 5 3 0
Capacity = 4. Index = 2
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[2] + dp[2- 1][4-weight[1]] >dp[2-1][4]
10 + 1 > 7
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6 7 7 7 7 7
10 3 2 0 1 6 10 11 16
16 5 3 0
Capacity = 5, Index = 2
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[2] + dp[2- 1][5-weight[1]] >dp[2-1][5]
10 + 6 > 7
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6 7 7 7 7 7
10 3 2 0 1 6 10 11 16 17
16 5 3 0
Capacity = 6, Index = 2
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[2] + dp[2- 1][6-weight[1]] >dp[2-1][6]
10 + 7 > 7
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6 7 7 7 7 7
10 3 2 0 1 6 10 11 16 17 17
16 5 3 0
Capacity = 7, Index = 2
the max weight till index 2 is 6, so the profit will be same as of
capacity=6, index=2
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6 7 7 7 7 7
10 3 2 0 1 6 10 11 16 17 17
16 5 3 0 1 6 10 11 16
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6 7 7 7 7 7
10 3 2 0 1 6 10 11 16 17 17
16 5 3 0 1 6 10 11 16 17
Capacity = 6, Index = 3
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[3] + dp[3- 1][6-weight[3]] == dp[3-1][6]
16 + 1 == 17
Capacity
1 1 0 0 1 1 1 1 1 1 1
6 2 1 0 1 6 7 7 7 7 7
10 3 2 0 1 6 10 11 16 17 17
16 5 3 0 1 6 10 11 16 17 22
Capacity = 7, Index = 3
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[3] + dp[3- 1][7-weight[3]] >dp[3-1][7]
16 + 6 > 17