~/ learn/ comp-372/ cards/ Implementing & analyzing OilKnapsack
1 of 5

OilKnapsack reconstruction loop (type it)

OilKnapsack reconstruction loop (type it)

Answer

filled, b = [], b_star for i in range(n, 0, -1): if best[i][b] != best[i - 1][b]: # order i changed the value -> it was filled filled.append(i) b -= qty[i - 1] filled.reverse()

Trace back from the winning sold-amount b_star: a row that changed the optimal value is an order that was filled. Drop b by that order’s barrels and continue. O(n) total.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/50b0ebf0-80e0-444e-9070-6a0a1607ea20/flashcard utf-8 LF