Quick Overview

Buy one blue, green or red shirt on each of n days without buying the same color on consecutive days, and return the color sequence with the lowest total cost. The task asks for the actual sequence rather than only the cost, testing optimization over sequences and reconstruction of the unique optimal choice.

Cheapest Shirt Color Sequence With No Color Repeated on Consecutive Days

Company: Elevenlabs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

A store runs a sale on blue, green and red shirts for the next `n` days. On day `i`, a blue shirt costs `blue_costs[i]`, a green shirt costs `green_costs[i]`, and a red shirt costs `red_costs[i]`. You buy exactly one shirt on each of the `n` days, but you may not buy the same color on two consecutive days. Return the color you buy on each day so that the total cost of the `n` shirts is as small as possible. ### Function Signature ```python def lowest_cost(blue_costs: list[int], green_costs: list[int], red_costs: list[int]) -> list[str]: ``` ### Rules - Return a list of length `n` whose `i`-th element is the color bought on day `i`: `'b'` for blue, `'g'` for green, or `'r'` for red. - For every `i` from `1` to `n - 1`, the colors bought on day `i - 1` and day `i` must differ. A color may repeat on days that are not consecutive. - The inputs are guaranteed to have exactly one valid color sequence with the minimum total cost, so the expected output is unique. ### Constraints - The three lists have the same length `n`. - `1 <= n <= 10^5` (the original only says the lists have length `n`; this bound is assumed for this practice version) - Every cost is a positive integer; assume `1 <= cost <= 10^4`, so every total fits in a 32-bit signed integer. ### Examples **Example 1** ```text Input: blue_costs = [7, 3, 8, 2], green_costs = [2, 9, 4, 6], red_costs = [5, 1, 9, 7] Output: ['g', 'r', 'g', 'b'] ``` The total is 2 + 1 + 4 + 2 = 9, and every other valid sequence costs more than 9. **Example 2** ```text Input: blue_costs = [1, 2], green_costs = [2, 100], red_costs = [3, 100] Output: ['g', 'b'] ``` The total is 2 + 2 = 4. The other valid sequences cost 101 (blue, green), 101 (blue, red), 102 (green, red), 5 (red, blue) and 103 (red, green). **Example 3** ```text Input: blue_costs = [5], green_costs = [3], red_costs = [4] Output: ['g'] ``` With a single day, the cheapest shirt is the green one.

Overview: Buy one blue, green or red shirt on each of n days without buying the same color on consecutive days, and return the color sequence with the lowest total cost. The task asks for the actual sequence rather than only the cost, testing optimization over sequences and reconstruction of the unique optimal choice.

Read the full Elevenlabs Software Engineer interview experience this question came from

A store runs a sale on blue, green and red shirts for the next `n` days. On day `i` (days are numbered from `0`), a blue shirt costs `blue_costs[i]`, a green shirt costs `green_costs[i]`, and a red shirt costs `red_costs[i]`. You buy exactly one shirt on each of the `n` days, but you may not buy the same color on two consecutive days. Return the color you buy on each day so that the total cost of the `n` shirts is as small as possible. Implement `lowest_cost(blue_costs, green_costs, red_costs)`. ### Output - Return a list of length `n` whose `i`-th element is the color bought on day `i`: `'b'` for blue, `'g'` for green, or `'r'` for red. - For every `i` from `1` to `n - 1`, the colors bought on day `i - 1` and day `i` must differ. A color may repeat on days that are not consecutive. - The inputs are guaranteed to have exactly one valid color sequence with the minimum total cost, so the expected output is unique. ### Constraints - The three lists have the same length `n`. - `1 <= n <= 10^5` (the original only says the lists have length `n`; this bound is assumed for this practice version). - Every cost is a positive integer; assume `1 <= cost <= 10^4`, so every total is at most `10^9` and fits in a 32-bit signed integer (no total exceeds `2^31 - 1`). ### Example 1 ```text Input: blue_costs = [7, 3, 8, 2], green_costs = [2, 9, 4, 6], red_costs = [5, 1, 9, 7] Output: ['g', 'r', 'g', 'b'] ``` The total is 2 + 1 + 4 + 2 = 9, and every other valid sequence costs more than 9. ### Example 2 ```text Input: blue_costs = [1, 2], green_costs = [2, 100], red_costs = [3, 100] Output: ['g', 'b'] ``` The total is 2 + 2 = 4. The other valid sequences cost 101 (blue, green), 101 (blue, red), 102 (green, red), 5 (red, blue) and 103 (red, green).

Constraints

  • The three lists blue_costs, green_costs and red_costs have the same length n.
  • 1 <= n <= 10^5 (the original only says the lists have length n; this bound is assumed for this practice version).
  • Every cost is a positive integer; assume 1 <= cost <= 10^4, so every total fits in a 32-bit signed integer (at most 10^9, never above 2^31 - 1).
  • The inputs are guaranteed to have exactly one valid color sequence with the minimum total cost, so the expected output is unique.

Examples

Input: ([7, 3, 8, 2], [2, 9, 4, 6], [5, 1, 9, 7])

Expected Output: ['g', 'r', 'g', 'b']

Explanation: Sealed Example 1: each day's cheapest color (green, red, green, blue) already alternates; total 2 + 1 + 4 + 2 = 9.

Input: ([1, 2], [2, 100], [3, 100])

Expected Output: ['g', 'b']

Explanation: Sealed Example 2, n = 2: day 0's cheapest color (blue) is given up so day 1 can take blue; total 2 + 2 = 4.

Hints

  1. Taking the cheapest color on every day on its own can buy the same color on two consecutive days, and fixing one clash can make a neighbouring day more expensive.
  2. When you decide a day's color, ask what you need to remember about the days already planned for that choice to be both legal and as cheap as possible.
  3. The answer is the list of colors, not only the minimum total, so keep enough information to recover which choice was made on each day.

Loading coding console...

Show the approach

Approach

Dynamic programming over the days, where the only state carried from one day to the next is the color bought the day before. Let best[i][c] be the minimum total cost of buying shirts on days 0..i with color c on day i. Then best[0][c] is day 0's price of color c, and for i >= 1, best[i][c] = price[c][i] + min(best[i-1][p]) over the two colors p != c; the predecessor p that achieves the minimum is stored. Invariant: after day i, best[i][c] is the cost of the cheapest valid plan for days 0..i that ends in color c, because every such plan is a valid plan for days 0..i-1 ending in a different color, followed by c, and every such extension is valid. The minimum total is the smallest of the three values for day n-1. Starting from that last-day color and following the stored predecessors back to day 0 rebuilds a sequence whose cost telescopes to that minimum and whose consecutive colors always differ, because each predecessor was chosen among the colors different from its successor. The input guarantees exactly one optimal sequence, so no tie can arise along the rebuilt path, and the result is that unique sequence. Edge cases: with n = 1 the answer is the single cheapest color; a day's cheapest color is often not used because a neighbouring day needs it more, so choosing each day's cheapest color, or the cheapest color that differs from the previous day, can be invalid or suboptimal; the last day's color need not be that day's cheapest. Totals are at most 10^5 * 10^4 = 10^9, inside the 32-bit signed range; the Java and C++ references still accumulate in 64-bit integers.

Time complexity:
O(n)
Space complexity:
O(n)