Booth Assignment Plan: Cheapest Non-Repeating Color Sequence
Dynamic ProgrammingAuto-graded · 21 test casesLast updated
Choose one of three colors per day to minimize total cost with no color on two adjacent days, then return the actual sequence using DP with backtracking.
A workshop finishes one product per day for n consecutive days. Each product must be coated in exactly one of three colors: 'slate', 'ember', or 'moss'.
The price of coating the product on day i depends on the color:
'slate'costsslate_price[i]'ember'costsember_price[i]'moss'costsmoss_price[i]
Days are indexed from 0. The same color may not be used on two consecutive days.
Write a function that returns the color chosen for every day so that the total price over all n days is as low as possible.
Output format
- Return a list of
nstrings. Elementiis the color for dayi, and must be exactly'slate','ember', or'moss'. - No two neighboring elements may be equal.
- The sum of the chosen prices must be the minimum over all valid color sequences.
- The input is guaranteed to have exactly one valid sequence that reaches the minimum total, so no tie-breaking rule is needed.
Constraints
1 <= n <= 10^5, and all three lists have lengthn.1 <= price <= 10^4for every entry.- Exactly one valid color sequence achieves the minimum total price.
- When
n == 1, there is no adjacency restriction, so the answer is the single cheapest color.
Examples
Example 1
Input: plan_booth_sequence([1, 1, 1], [3, 5, 7], [4, 6, 4])
Output: ['slate', 'ember', 'slate']
Example 2
Input: plan_booth_sequence([18, 12, 1, 9], [13, 15, 7, 9], [12, 16, 4, 8])
Output: ['moss', 'ember', 'slate', 'moss']
Example 3
Input: plan_booth_sequence([100, 1, 76, 14], [22, 20, 1, 2], [99, 99, 5, 12])
Output: ['ember', 'slate', 'moss', 'ember']
Hints
Hint 1
Picking the cheapest allowed color each day can backfire, because a choice today restricts tomorrow. Think about tracking the best total cost for each possible color on the current day.
Hint 2
Let best[c] be the minimum cost of a valid plan for days 0..i that ends in color c. The value for color c on day i is its price plus the smaller of the two best values for the other colors on day i-1.
Hint 3
To recover the actual colors, store for every day and color which previous color gave the minimum. Start from the cheapest final color and follow these links backward, then reverse the result.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Choosing greedily (the cheapest allowed color each day) can fail, because a cheap choice today may force an expensive one tomorrow. Instead, use dynamic programming over days with only three states per day.
State. best[c] is the minimum total price of a valid plan for days 0..i in which day i uses color c.
Base case. On day 0, best[c] is just the price of color c on that day.
Transition. For each later day and each color c:
new_best[c] = price[c][day] + min(best[o] for o != c)
Only the two other colors are candidates, so this is a constant amount of work. Along with the value, record in back[day][c] which previous color achieved that minimum.
Reconstruction. After the last day, the cheapest entry in best gives the final color and the minimum total. Walk backward: write down the current color, then jump to back[day][color], until day 0 is reached. Each day's color is read off in reverse order, so the list is filled from the end.
Why it is correct
Any valid plan ending in color c on day i consists of a valid plan for days 0..i-1 ending in some color other than c, plus the price of c on day i. The cost of the first part is independent of how day i is handled, so using the cheapest such prefix is always at least as good. This optimal substructure means each best[c] is truly the minimum. The back pointers record exactly the prefix that was used, so following them reproduces a plan with the minimum total. Since the optimal sequence is unique, it is the one returned.
Complexity
- Time:
O(n), with a constant factor of about 3 colors × 2 alternatives per day. - Space:
O(n)for the back pointers, which are needed to rebuild the sequence.
Reference solution
from typing import List
def plan_booth_sequence(slate_price: List[int], ember_price: List[int], moss_price: List[int]) -> List[str]:
names = ("slate", "ember", "moss")
prices = (slate_price, ember_price, moss_price)
n = len(slate_price)
best = [prices[c][0] for c in range(3)]
back = [[-1] * 3 for _ in range(n)]
for day in range(1, n):
nxt = [0, 0, 0]
for c in range(3):
prev_cost, prev_color = min(
(best[o], o) for o in range(3) if o != c
)
nxt[c] = prices[c][day] + prev_cost
back[day][c] = prev_color
best = nxt
color = min(range(3), key=lambda c: best[c])
result = [""] * n
for day in range(n - 1, -1, -1):
result[day] = names[color]
color = back[day][color]
return result