Minimum Cost for a Target Weight
Updated
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.
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.
Similar questions at other companies
- Jump GamePaytm · PHONE SCREENPaytm interview questions
- Jump Game IINavi · PHONE SCREEN
- Best Stock Profit With a Sell FeeKaseya · PHONE SCREEN
- Fewest Jumps to the End or StuckAdobe · PHONE SCREENAdobe interview questions
- Return a Minimum-Coin Change BreakdownDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Most Relevant Text SpanHebbia · PHONE SCREEN
- Jump GameZillow · PHONE SCREENZillow interview questions
- Fewest Jumps to the End of an ArrayRippling · PHONE SCREENRippling interview questions
- Split Array to Minimize the Largest SumNation with NaMo · PHONE SCREEN
- Minimum Racks for Server ResourcesGoogle · PHONE SCREENGoogle interview questions