High+low-stress job recurrence (+ proof) [00QY]
The key to solving this problem is to come up with a recursive solution, and then use dynamic programming. The key idea (common to many similar problems) is to consider the most we could make if we worked only through week i and then stopped. That is, define \mathit {OPT}(i) = maximum revenue for working weeks 1 \ldots i. Now, we can come up with a recurrence for \mathit {OPT}:
- Base cases:
- \mathit {OPT}(0) = 0. We don’t make any money for working 0 weeks.
- \mathit {OPT}(1) = L_1. We can’t take the high-stress job the first week. (Or maybe we can, in which case \mathit {OPT}(1) = \max (L_1, H_1)—we need to clarify the problem parameters!)
- As our inductive/recursive case, \mathit {OPT}(k) = \max \begin {cases} L_{k} + \mathit {OPT}(k-1) \\ H_{k} + \mathit {OPT}(k-2)\end {cases} First, note that if we are going to maximize the profit for the first k weeks, we should always work the final week (there's no reason to take the week off to prepare for anything; we could always at the very least take the low-stress job). Thus, our decision is between working a high-stress or low-stress job in the final week. Choosing a low-stress job at week k means we should add L_{k} to the optimal profit for the previous k-1 weeks and choosing a high-stress job at week k means we had to take week k-1 off so we should add H_{k} to \mathit {OPT}(k-2). The optimal choice is to choose the max of these two options.
Notice that our arguments above actually constitute a proof by strong induction of the recurrence's correctness: we first argue why the base case(s) are correct, then argue why the recursive case for \mathit {OPT}(k) is correct (assuming, as our induction hypothesis, that \mathit {OPT} is already correct for any inputs < k).