Problem · Array
Minimum-Cost Solar Rooftop Allocation
Learn this problemProblem statement
A colony has n houses. House i requires requirements[i] units of energy. Energy produced by any rooftop enters a shared grid and may satisfy any house.
You may install at most one rooftop on each house:
- A type-one rooftop costs
typeOneCostand produces exactlyrequirements[i]units on housei. - A type-two rooftop costs
typeTwoCostand produces exactly2 * requirements[i]units on housei. - You may also leave a house without a rooftop.
Choose rooftops whose total production is at least the colony's total requirement. Return the minimum possible installation cost.
Function
minimumSolarCost(requirements: long[], typeOneCost: long, typeTwoCost: long) → longExamples
Example 1
requirements = [4,2,1]typeOneCost = 5typeTwoCost = 8return = 8The colony needs 7 units. One type-two rooftop on the house requiring 4 units produces 8 units at cost 8.
Example 2
requirements = [3,3,3]typeOneCost = 4typeTwoCost = 10return = 12Three type-one rooftops produce exactly 9 units for cost 12, which is cheaper than every sufficient combination using type two.
Example 3
requirements = [10,1]typeOneCost = 100typeTwoCost = 3return = 3A type-two rooftop on the first house produces 20 units, enough for the total requirement of 11, at cost 3.
Constraints
1 <= requirements.length <= 1000001 <= requirements[i] <= 10^91 <= typeOneCost, typeTwoCost <= 10^9- The answer fits in a signed 64-bit integer.