KatInterview

Daily Vendor Booking with No Immediate Repeats

Dynamic ProgrammingAuto-graded · 18 test cases
HardAuto-gradedDynamic Programmingdynamic-programmingpath-reconstructionoptimization

Last updated

Choose one vendor per day to minimize total price with no vendor on consecutive days, returning the plan with a fixed tie-break; tests DP, backtracking, and best/second-best.

A café books exactly one vendor every morning from a fixed list. Each vendor quotes a price for each day, and prices may differ from day to day. The café has one fairness rule: the same vendor may not be booked on two consecutive days. A vendor may be booked again after a gap of at least one day.

You are given a table prices, where prices[d][v] is the cost of booking vendor v on day d. Both days and vendors are numbered from 0. Find the cheapest valid plan and return its total cost together with the plan itself.

Task

Implement a function that takes prices and returns a pair (total, plan):

  • total is the minimum possible sum of prices[d][plan[d]] over all days d, taken over all valid plans.
  • plan is a list of n = len(prices) integers, where plan[d] is the vendor index booked on day d.
  • A plan is valid when plan[d] != plan[d + 1] for every d from 0 to n - 2.

Tie-breaking rule

Several valid plans may share the minimum total. Return exactly one of them, chosen as follows. Compare the tied plans by their last-day vendor and prefer the smaller index. If still tied, compare the second-to-last day and prefer the smaller index, then the third-to-last day, and so on. In other words, among all minimum-cost valid plans, return the one that is lexicographically smallest when the plan is read from the last day back to the first day.

Special cases

  • If n == 1, return the cheapest vendor on that single day. On equal prices, take the smallest index.
  • If prices is empty, return (0, []).
  • If there is only one vendor (k == 1), the "no consecutive repeat" rule cannot be satisfied for n > 1. In that case, ignore the rule and return (sum of all prices, [0] * n).

Efficiency

The number of vendors k can be as large as 100, and n as large as 100000, so aim for O(n * k) time rather than O(n * k^2).

Constraints

  • 0 <= n <= 10^5, where n = len(prices)
  • 1 <= k <= 100, where k = len(prices[d]); every row has the same length
  • 1 <= prices[d][v] <= 10^4
  • The returned value is a pair (total, plan) with total an integer and plan a list of integers.

Examples

Example 1

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

Output: [11, [1, 0, 2, 1]]

Example 2

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

Output: [6, [1, 0, 1]]

Hints

Hint 1

Only the vendor used yesterday restricts today's choice, so define the state as (day, vendor booked that day) holding the cheapest cost of the prefix ending that way.

Hint 2

To make each day O(k), you do not need to scan every previous vendor for every current one. Which previous-day values can ever be the best predecessor?

Hint 3

Keep the smallest and second-smallest previous-day totals, ranking by (total, vendor index). Use the smallest unless it is the same vendor, in which case use the second smallest. Store the chosen predecessor, then walk back from the best last vendor (smallest total, ties by smaller index).