Dynamic programming example: high+low stress jobs [00QV]
- October 28, 2026
Let's see how to apply this dynamic programming framework to a simple yet nontrivial problem. (The Fibonacci number example we started with was simple, yet also trivial, in the sense that we don't really need dynamic programming to compute Fibonacci numbers.)
Introduction [00QW]
- October 28, 2026
Consider the following table composed of n weeks where each week i has low stress job that pays L_{i} and a high stress job that pays H_{i}.
| WEEK | 1 | 2 | 3 | \dots | n |
|---|---|---|---|---|---|
| low stress | L_{1} | L_{2} | L_{3} | \dots | L_{n} |
| high stress | H_{1} | H_{2} | H_{3} | \dots | H_{n} |
Each week you are allowed to pick either a low stress job or a high stress job; however, picking a high stress job at week i means that you must first take the week before (i.e. week i-1) off. Your goal is to maximize your total income. Let’s suppose that you are allowed to take the high-stress job in week 1 (it doesn’t change the overall solution much either way, only the base case).
Greedy attempt [00QX]
- October 28, 2026
We've previously studied greedy algorithms, which is definitely the thing to try first. For example, one possible greedy algorithm is shown below.
This algorithm basically looks ahead one week and decides if it’s worth taking a week off in order to land a higher-paying job. However, this strategy fails, because taking a high stress job at week h_{i} means that you cannot take week h_{i} off to take a job at week h_{i+1}. Thus, any strategy employing look-ahead by a constant factor will fail. Here is a counter-example to the greedy algorithm.
| WEEK | 1 | 2 | 3 |
|---|---|---|---|
| low stress | 2 | 2 | 1 |
| high stress | 1 | 5 | 10 |
- The greedy algorithm chooses: 5 (week 2) + 1 (week 3) = 6
- However, the optimal solution is to choose: 2 (week 1) + 10 (week 3) = 12
High+low-stress job recurrence (+ proof) [00QY]
- October 28, 2026
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).
Memoization and analysis [00QZ]
- October 28, 2026
We can see that naively computing \mathit {OPT}(n) using recursion would lead to many values of \mathit {OPT} being redundantly computed over and over—in fact, it has the same recursion pattern as the Fibonacci numbers. Thus, we must memoize.
Notice that \mathit {OPT} naturally forms a 1 \times (n+1) table where each entry requires \Theta (1) operations to fill in. We can use a recursive function that fills in the table as it goes, or we can just fill in the table using a loop from 0 up to n. Either way, the whole algorithm is obviously \Theta (n). This is one nice benefit of dynamic programming—analyzing the runtime of an algorithm using dynamic programming is usually dead easy!
Sample Python code for high+low-stress jobs [00R0]
- October 29, 2026
Here is how we might implement this algorithm in Python:
# low, high are lists of weekly salaries for low- and
# high-stress job, respectively.
def best_income(low, high):
n = len(low)
# Make low and high 1-indexed by adding dummy values to the
# beginning. This works much more nicely since we want M[i] =
# maximum for working i weeks, and M[0] = 0 is a natural base
# case, so we want to have the weeks be 1-indexed. (Adding extra
# "padding" elements to arrays to make room for base cases is a
# common technique with DP problems.)
low = [0] + low
high = [0] + high
# Initialize OPT[0..n] with all zeros.
OPT = [0] * (n+1)
# The first base case, OPT[0] = 0, is already taken care of.
# For OPT[1], just take the higher of the two jobs.
OPT[1] = max(low[1], high[1])
# Now loop through remaining weeks.
for i in range(2, n+1):
# How much could we make taking the low or high stress job?
low_total = low[i] + OPT[i-1]
high_total = high[i] + OPT[i-2]
# The optimal for weeks 1..i is the higher of the two
OPT[i] = max(low_total, high_total)
return OPT[n]
# Example:
# >>> best_income([2,2,1,7,5,20,3,19,10,13], [1,5,10,100,23,20,5,21,30,30])
# 182Remembering choices to reconstruct optimal job schedule [00R1]
- October 29, 2026
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].
Sample Python code for high+low-stress jobs with optimal schedule reconstruction [00R2]
- October 29, 2026
Here is an enhanced version of the Python solution which computes the optimal schedule along with the optimal income:
def best_income(low, high):
n = len(low)
low = [0] + low
high = [0] + high
# Initialize an array of booleans to keep track of whether the
# optimal choice is to take the high-stress job at week i (if we
# only work weeks 1..i).
take_high_job = [False] * (n+1)
OPT = [0] * (n+1)
OPT[1] = max(low[1], high[1])
# We should take the high-stress job at week 1 iff it pays more.
take_high_job[1] = (high[1] > low[1])
for i in range(2, n+1):
low_total = low[i] + OPT[i-1]
high_total = high[i] + OPT[i-2]
OPT[i] = max(low_total, high_total)
# Record which choice produced the higher total
take_high_job[i] = high_total > low_total
# Now, to produce a work schedule, work backwards from the end
# Start at week n
w = n
schedule = []
while w > 0:
# If we should take the high-stress job at week w, schedule it
# and a week off, and proceed to look at week w-2 next
if take_high_job[w]:
schedule = ['off', 'HI'] + schedule
w -= 2
# Otherwise schedule the low-stress job and look at week w-1 next
else:
schedule = ['LO'] + schedule
w -= 1
return (OPT[n], schedule)
# Example:
# >>> best_income([2,2,1,7,5,20,3,19,10,13], [1,5,10,100,23,20,5,21,30,30])
# (182, ['off', 'HI', 'off', 'HI', 'LO', 'LO', 'LO', 'LO', 'off', 'HI'])