Minimum Cost for a Target Weight
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) → longExamples
Example 1
cost = [2,5,7,11,25]minWeight = 26return = 37Buying three weight-8 items and two weight-1 items reaches weight 26 for cost 37.
Example 2
cost = [3,10,20]minWeight = 5return = 15Five 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.