Problem · Heap
Items Purchase (For Java Backend Engineer)
n items where the price of the ith item is price[i]. A frequent customer has m discount coupons. If x discount coupons are used on the ith item, its price is reduced to the integer floor(price(i) / 2^x), e.g. floor(3/2^1) = floor(1.5) = 1.
Find the minimum amount needed to purchase all the items of the shop using at most m coupons.
Complete the function findMinimumPrice in the editor below.
findMinimumPrice has the following parameters:
int price[n]: the original prices of the itemsint m: the number of discount coupons
Returns
int: the minimum amount needed to purchase all items
Examples
01 · Example 1
price = [2, 4] m = 2 return = 3
The optimum solution:
- Purchase item 1 for 2.
- Use 2 coupons on item 2, so the discounted price is 4/2^2 = 4 / 4 = 1.
Constraints
1 <= n <= 10^50 <= m <= 10^91 <= price[i] <= 10^9More Paypal problems
public int findMinimumPrice(int[] price, int m) {
// write your code here
}
price[2, 4]
m2
expected3
sign in to submit