Solar Inverter Day Plan
Minimum cost from a two-column array with penalties for changing columns
A solar farm can route its daily production through either inverter A or inverter B. Exactly one inverter must be used for each scheduled day.
You are given costs, where costs[i][0] is the operating cost of using A on day i, and costs[i][1] is the operating cost of using B on that day. Days appear in chronological order.
Whenever two consecutive days use different inverters, the farm pays an additional switch_cost. There is no switching charge before the first day or after the last day.
Return the minimum total cost of operating all scheduled days, including switching charges. If there are no scheduled days, return 0.
Examples
Example 1
Input: costs = [[1,8],[7,1],[6,2]], switch_cost = 3 Output: 7
Using A on the first day and B on the next two days gives operating costs of 1, 1, and 2, plus one switching charge of 3. No other plan has a lower total cost.
Example 2
Input: costs = [[1,4],[4,1]], switch_cost = 10 Output: 5
Although the cheaper inverter changes between days, the switching charge makes it better to keep the same inverter throughout. Either all-A or all-B is optimal.
Example 3
Input: costs = [], switch_cost = 5 Output: 0
There are no scheduled days, so there are no operating costs or switching charges.
Constraints
- 0 <= costs.length <= 6000
- Each row of costs contains exactly two integers.
- 0 <= costs[i][j] <= 1000000
- 0 <= switch_cost <= 1000000
The intended solution takes O(n) time and O(1) auxiliary space, where n is the number of scheduled days.
Hints
Show hint 1Hint 1
After processing a day, keep separate minimum totals for plans ending with A and plans ending with B.
Show hint 2Hint 2
To end the next day with a particular inverter, compare staying with it against switching from the other inverter.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you also return an optimal inverter sequence, choosing the lexicographically smallest sequence with A before B when costs tie?
- How would the recurrence change if each boundary between consecutive days had its own switching charge?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem