Remembering choices to reconstruct optimal job schedule [00R1]

This solution tells us how much money we can make, but it doesn’t actually tell us which job we should take each week. However, it is not too hard to modify our solution to do this as well. The key observation is that during the algorithm, for each i, taking the max of two values corresponds to deciding which option would be better—to take the low- or high-stress job at week i. So we can just add another array, \mathit {JOBS}, to keep track of which decision leads to the best outcome at each week—that is, \mathit {JOBS}[i] records whether we should pick the low- or high-stress job at week i in order to maximize our income if we only work weeks 1 \dots i.

Note that \mathit {JOBS} does not directly tell us which jobs we should actually take to get the most money overall. For example, suppose \mathit {JOBS}[3] contains H. This means that if we only worked the first three weeks, then we should take the high-stress job in week 3. However, it may turn out that the best schedule overall has us working the low-stress job or taking week 3 off.

So, how do we actually reconstruct the optimal schedule for weeks 1 \dots n from \mathit {JOBS}? We just start at week n and work backwards, mirroring the recursive structure of \mathit {OPT}:

  • If \mathit {JOBS}[n] = L, then we take the low-stress job in week n, and continue by looking at \mathit {JOBS}[n-1] to see what we should do the week before.
  • If \mathit {JOBS}[n] = H, then we take the high-stress job in week n, take week n-1 off, and continue by looking at \mathit {JOBS}[n-2].
We continue this process until reaching week 0.