FastPrepMinimum Cost to Reach a Target Weight

Minimum Cost to Reach a Target Weight

Chubb logoChubb● HardNEW GRADOA
Learn

Problem statement

Item type i has weight 2^i and costs cost[i]. You may buy any nonnegative number of every type.

Return the minimum total cost whose purchased weight is at least minWeight.

Function

minimumWeightCost(cost: int[], minWeight: int) → long

Examples

Example 1

cost = [2,5,7,11,25]minWeight = 26return = 37

Normalization and the high-to-low scan compare exact coverage with the cheapest permissible overshoot.

Example 2

cost = [1]minWeight = 0return = 0

Normalization and the high-to-low scan compare exact coverage with the cheapest permissible overshoot.

Example 3

cost = [3]minWeight = 5return = 15

Normalization and the high-to-low scan compare exact coverage with the cheapest permissible overshoot.

Constraints

  • 1 <= cost.length <= 31.
  • 1 <= cost[i] <= 10^9.
  • 0 <= minWeight <= 10^9.
  • The answer fits a signed 64-bit integer.

Source note: Author-written prompt excerpt retained for source fidelity.

More Chubb problems

See Chubb hiring insights
public long minimumWeightCost(int[] cost, int minWeight) {
    // Write your code here.
}
cost[2,5,7,11,25]
minWeight26
expected37
Checking account…