Minimum Number of Coins for Fruits - Problem

You are given a 0-indexed integer array prices where prices[i] denotes the number of coins needed to purchase the (i + 1)th fruit.

The fruit market has the following reward for each fruit:

  • If you purchase the (i + 1)th fruit at prices[i] coins, you can get any number of the next i fruits for free.

Note: Even if you can take fruit j for free, you can still purchase it for prices[j - 1] coins to receive its reward.

Return the minimum number of coins needed to acquire all the fruits.

Input & Output

Example 1 — Basic Case
$ Input: prices = [3,1,2]
Output: 4
💡 Note: Buy fruit 1 (cost 3, get fruit 2 free), then buy fruit 3 (cost 2). Total = 3 + 1 = 4. Alternatively: buy fruit 2 (cost 1, no free fruits), buy fruit 3 (cost 2, no free fruits), buy fruit 1 (cost 3, get next 1 fruit free but already have all). Total = 1 + 2 + 0 = 3. Wait, let me recalculate: Buy fruit 2 (cost 1), buy fruit 1 (cost 3, get next 1 fruit free which covers fruit 2 retroactively? No). Actually: Buy fruit 1 (cost 3, get fruit 2 free), buy fruit 3 (cost 2). Total = 5. Or buy fruit 2 (cost 1), buy fruit 3 (cost 2, get next 2 fruits free but there aren't any). Total cost = 1 + 2 = 3. But we still need fruit 1, so buy it for 3. Total = 6. Actually optimal: buy fruit 2 (index 1, cost 1), buy fruit 1 (index 0, cost 3, get next 0 fruits free - none). Total = 4.
Example 2 — Single Fruit
$ Input: prices = [1]
Output: 1
💡 Note: Only one fruit to buy, costs 1 coin. Total = 1.
Example 3 — Two Fruits
$ Input: prices = [1,10]
Output: 2
💡 Note: Buy fruit 1 (cost 1, get next 1 fruit free), so fruit 2 is free. Total = 1. Wait, that's wrong indexing. Buy fruit at index 0 (cost 1, get next 0 fruits free - none), then buy fruit at index 1 (cost 10). Total = 11. OR buy fruit at index 1 (cost 10, get next 1 fruit free, but there are no more fruits). So we still need fruit at index 0, costing 1. Total = 11. Actually optimal is: buy fruit 1 (index 0) for cost 1, buy fruit 2 (index 1) for cost 10. But wait, if we buy fruit 2 first (cost 10, get next 1 fruit free), we can't get fruit 1 free because it's before. So buy fruit 1 (cost 1, get 0 next fruits free), then buy fruit 2 (cost 10). Total = 11. But there might be a better way: we must buy fruit 1 for 1, and fruit 2 for 10, so minimum is 11. Hmm, let me reconsider the problem... Actually, I think the answer should be 2 if we can somehow get fruit 2 cheaper.

Constraints

  • 1 ≤ prices.length ≤ 1000
  • 1 ≤ prices[i] ≤ 105

Visualization

Tap to expand
Minimum Number of Coins for Fruits INPUT prices array (0-indexed) 3 i=0 1 i=1 2 i=2 Fruit 1 Fruit 2 Fruit 3 Reward Rules: Buy fruit i+1 at prices[i] Get next i fruits FREE Or buy them for bonus prices = [3, 1, 2] n = 3 fruits total ALGORITHM STEPS 1 Initialize DP + Deque dp[i] = min cost to get all fruits from i to end 2 Process Right to Left For i from n-1 to 0 Use deque for min query 3 Transition Formula dp[i] = prices[i] + min(dp[i+1...2i+2]) 4 Return dp[0] Min cost starting fruit 1 DP Table (right to left): i=2: dp[2]=2 (buy fruit3) i=1: dp[1]=1+0=1 (free f3) i=0: dp[0]=3+1=4 (buy f1) Result: dp[0] = 4 FINAL RESULT Optimal Strategy Step 1: Buy Fruit 1 Pay 3 coins Get Fruit 2 FREE! Step 2: Buy Fruit 2 Pay 1 coin (for reward) Fruit 3 now FREE! OUTPUT 4 OK - Minimum coins = 4 All 3 fruits acquired! Key Insight: Use a monotonic deque to efficiently find minimum dp value in the range [i+1, 2i+2]. Buying a fruit at index i gives next i fruits free, but buying them instead can unlock more free fruits. Optimized from O(n^2) brute force DP to O(n) with deque. Trade-off: pay now for larger free window later. TutorialsPoint - Minimum Number of Coins for Fruits | Optimized DP with Deque Approach
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~25 min Avg. Time
428 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen