Cheapest Daily Ticket Plan Without Back-to-Back Repeats
Dynamic ProgrammingAuto-graded · 25 test casesLast updated
Choose one of three item types each day with no type repeated on adjacent days, minimizing total cost and returning the unique optimal sequence of choices.
A shop offers items in three colors: blue, green and red. You will visit it on each of n consecutive days and must buy exactly one item per day. On day i (0-indexed), the blue item costs blue_costs[i], the green item costs green_costs[i], and the red item costs red_costs[i].
You may not buy the same color on two consecutive days. Find the plan with the smallest total cost and return the color chosen on each day.
Output format
Return a list of n single-character strings, where element i is the color bought on day i:
'b'for blue'g'for green'r'for red
Rules
- For every
ifrom1ton - 1, the colors on dayi - 1and dayimust differ. - The same color may appear again on days that are not adjacent (for example green, red, green is allowed).
- The input is guaranteed to have exactly one valid color sequence with the minimum total cost, so the expected answer is unique and no tie-breaking is needed.
Constraints
- The three lists all have the same length
n. 1 <= n <= 10^51 <= cost <= 10^4for every entry.- The minimum total fits comfortably in a 32-bit signed integer.
Examples
Example 1
Input: cheapest_color_plan([7, 3, 8, 2], [2, 9, 4, 6], [5, 1, 9, 7])
Output: ['g', 'r', 'g', 'b']
Example 2
Input: cheapest_color_plan([1, 2], [2, 100], [3, 100])
Output: ['g', 'b']
Example 3
Input: cheapest_color_plan([5], [3], [4])
Output: ['g']
Hints
Hint 1
Picking the cheapest color each day can fail, because a cheap choice today may force an expensive choice tomorrow. Think about what information about the past actually matters for the next day.
Hint 2
Let the state be the color bought on the current day. For each color, store the cheapest total cost of any valid plan for the days so far that ends in that color; each color extends the best of the other two colors from the previous day.
Hint 3
To recover the sequence rather than just the cost, record for each day and each color which previous color gave the best value. Start from the cheapest final color, follow these records backward, then reverse the result.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Whether a color is allowed today depends only on yesterday's color, so the state is simply "the color bought on the current day".
Let dp[c] be the minimum total cost of a valid plan for days 0..i whose day-i color is c. Then:
dp_i[blue] = blue_costs[i] + min(dp_{i-1}[green], dp_{i-1}[red])dp_i[green] = green_costs[i] + min(dp_{i-1}[blue], dp_{i-1}[red])dp_i[red] = red_costs[i] + min(dp_{i-1}[blue], dp_{i-1}[green])
The base case is day 0, where dp is just the three day-0 costs.
To rebuild the sequence, for each day and color we also store which previous color achieved the minimum. After the last day, pick the color with the smallest dp value, follow the stored predecessors back to day 0, and reverse the collected list.
Why it is correct
- Optimal substructure: the prefix of an optimal plan ending in color
c'on dayi-1must be optimal among plans ending inc'. Otherwise, swapping in a cheaper prefix with the same last color keeps the plan valid and lowers the total. - Complete state: the only constraint links adjacent days, so the last color captures everything relevant about the past.
- Ties: ties can only matter on branches that are not part of the optimal plan, because the problem guarantees a unique optimum. The path we follow is therefore always the unique optimal one.
Complexity
- Time: O(n), with constant work per day.
- Space: O(n) for the predecessor table (the
dpvalues themselves use O(1)).
Reference solution
def cheapest_color_plan(blue_costs, green_costs, red_costs):
n = len(blue_costs)
cols = 'bgr'
dp = [blue_costs[0], green_costs[0], red_costs[0]]
choice = []
for i in range(1, n):
b, g, r = dp
nb = blue_costs[i] + (g if g < r else r)
ng = green_costs[i] + (b if b < r else r)
nr = red_costs[i] + (b if b < g else g)
choice.append((1 if g < r else 2, 0 if b < r else 2, 0 if b < g else 1))
dp = [nb, ng, nr]
c = min(range(3), key=lambda x: dp[x])
res = [c]
for i in range(n - 2, -1, -1):
c = choice[i][c]
res.append(c)
res.reverse()
return [cols[x] for x in res]