FastPrepGet Minimum Cost

Get Minimum Cost

TikTok logoTikTok● EasyFULLTIMEOA

Problem statement

Alex is a TikTok influencer who loves shopping online and sharing his latest hauls with his followers. He's super excited about buying several products from an e-commerce website to feature in his next video. Each item on his shopping list has its own price, and Alex wants to show his followers how to get the best deals.

Alex has a number of discount vouchers that he can use on any of his purchases. These vouchers offer a unique discount mechanism: the more vouchers used on a single item, the greater the discount. For instance, for each voucher used on an item, the price of that item is halved. If multiple vouchers are used on the same item, the price is halved repeatedly.

The discount can be represented by the following formula: discounted price = p / 2^k

where:

  • p is the original price of the item.
  • k is the number of vouchers used on that item.

Function

getMinimumCost(vouchersCount: int, prices: int[]) → int

Complete the function getMinimumCost in the editor.

getMinimumCost has the following parameters:

  1. 1. int vouchersCount: the number of discount vouchers
  2. 2. int[] prices: an array of integers representing the prices of the items

Returns intint: the minimum cost after applying the vouchers

Examples

Example 1

vouchersCount = 3prices = [8, 2, 13]return = 9
  • 13 / 2^2 = 3
  • 8 / 2^1 = 4
  • 2 / 2^0 = 2

Total = 3 + 4 + 2 = 9.

Constraints

  • 🥭🥭

More TikTok problems

See TikTok hiring insights
Saved
public int getMinimumCost(int vouchersCount, int[] prices) {
  // write your code here
}
vouchersCount3
prices[8, 2, 13]
expected9
Checking account…