Solution: Maximum Amount of Gold

Solutions for the Maximum Amount of Gold Problem.

Solution 1: Analyzing the structure of a solution

Instead of solving the original problem, we will check whether it’s possible to fully pack our backpack with the gold bars. Given nn gold bars of weights w0,...,wn1w_0,...,w_{n−1} (we switched to 0-based indexing) and an integer WW, is it possible to select a subset of them of total weight WW?

Exercise break: Show how to use the solution to this problem to solve the Maximum Amount of Gold Problem.

Assume that it’s possible to fully pack the backpack. There exists a set Sw0,,wn1S ⊆ {w_0,\ldots,w_{n−1}} of total weight WW. Does it include the last bar of weight wn1w_{n−1}?

Case 1: If wn1∉Sw_{n−1} \not\in S, then a backpack of capacity WW can be fully packed using the first n1n − 1 bars.

Case 2: If wn1Sw_{n−1} ∈ S, then we can remove the bar of weight wn1w_{n−1} from the backpack and the remaining bars will have weight Wwn1W − w_{n−1}. Therefore, a backpack of capacity Wwn1W − w_{n−1} can be fully packed with the first n1n − 1 gold bars.

In both cases, we reduced the problem to essentially the same problem with a smaller number of items and possibly smaller backpack capacity. We therefore consider the variable pack(w,i)pack(w, i) equal to true if it is possible to fully pack a backpack of capacity ww using the first ii bars, and false, otherwise. The analysis of the two cases above leads to the following recurrence relation for i>0i > 0,

pack(w,i)=pack(w,i1)  or  pack(wwi1,i1).pack(w,i) = pack(w,i − 1)~~ \text{or}~~ pack(w − wi−1,i − 1).

Note that the second term in the above formula does not make sense when wi1>ww_{i−1} > w. Also, pack(0,0)=pack(0,0) = true, and pack(0,w)=pack(0,w) = false for any w>0w > 0. Overall,

pack(w,i)={trueif   i=0  and  w=0falseLCS(i,j,k1)LCS(i1,j1,k1)+1if   A[i]=B[j]=C[k]pack(w, i) = \begin{cases} \text{true} & \text{if }~~ i = 0 ~~\text{and}~~ w = 0 \\ \text{false} \\ LCS(i , j, k-1) \\ LCS(i-1, j-1, k-1)+1 & \text{if }~~ A[i]=B[j]=C[k] \end{cases} ...