FastPrepMinimum Cost for a Target Weight

Minimum Cost for a Target Weight

Pace Stock Broking Services logoPace Stock Broking Services● MediumINTERNPHONE SCREEN
Learn

Problem statement

You are given an integer array cost, where cost[i] is the price of one item weighing 2^i. You may buy any nonnegative number of items of every available weight.

Given minWeight, return the minimum total cost whose purchased weight is at least minWeight. The total weight may exceed the target.

Function

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

Examples

Example 1

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

Buying three weight-8 items and two weight-1 items reaches weight 26 for cost 37.

Example 2

cost = [3,10,20]minWeight = 5return = 15

Five weight-1 items cost 15. This corrects the reported sample's non-minimal total while preserving the task contract.

Constraints

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

Source note: The source image is a source-neutral excerpt of the author-reported prompt.

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