780. Minimum Number of Coins for Fruits II
Imagine you are visiting a vibrant fruit market known for its wide array of exotic fruits. Each fruit comes with a price tag, and you are provided with a 1-indexed array prices where prices[i] represents the number of coins needed to purchase the i-th fruit.
At this market, there is an enticing promotional offer:
- When you buy the i-th fruit at a cost of prices[i] coins, you are entitled to receive the next i fruits completely free of charge.
- Important: Even if a fruit is available for free under a previous offer, you still have the option to purchase it at its listed price to activate its own promotional offer.
Return the minimum total number of coins required to acquire all the fruits.
Example 1:
Input : prices = [3, 1, 2]
Output : 4
Explanation :
- Purchase the 1st fruit for 3 coins; this purchase allows you to take the 2nd fruit for free.
- Although the 2nd fruit was free, buying it for 1 coin activates its offer, which then gives you the 3rd fruit for free.
Total coins spent = 3 + 1 = 4.
Example 2:
Input : prices = [1, 10, 1, 1]
Output : 2
Explanation :
- Purchase the 1st fruit for 1 coin, which grants you the 2nd fruit for free.
- Purchase the 3rd fruit for 1 coin; this purchase gives you the 4th fruit for free.
Total coins spent = 1 + 1 = 2.
Now Your Turn!
Pick the correct output for the given inputInput : prices = [2, 2, 2, 2]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 ≤ prices.length ≤105
- 1 ≤ prices[i] ≤ 105