FastPrepMinimum-Cost Solar Rooftop Allocation
Problem · Array

Minimum-Cost Solar Rooftop Allocation

Learn this problem
MediumDeloitte logoDeloitteNEW GRADOA

Problem 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 typeOneCost and produces exactly requirements[i] units on house i.
  • A type-two rooftop costs typeTwoCost and produces exactly 2 * requirements[i] units on house i.
  • 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) → long

Examples

Example 1

requirements = [4,2,1]typeOneCost = 5typeTwoCost = 8return = 8

The 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 = 12

Three 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 = 3

A type-two rooftop on the first house produces 20 units, enough for the total requirement of 11, at cost 3.

Constraints

  • 1 <= requirements.length <= 100000
  • 1 <= requirements[i] <= 10^9
  • 1 <= typeOneCost, typeTwoCost <= 10^9
  • The answer fits in a signed 64-bit integer.

More Deloitte problems

drafts saved locally
public long minimumSolarCost(long[] requirements, long typeOneCost, long typeTwoCost) {
    // Write your solution here.
}
requirements[4,2,1]
typeOneCost5
typeTwoCost8
expected8
checking account