Minimum Number of Coins for Fruits II - Problem

Imagine you're shopping at an exotic fruit market where the vendor has an incredible offer! Each fruit comes with a special deal: when you buy the i-th fruit for prices[i] coins, you get the next i fruits completely free!

You're given a 1-indexed array prices where prices[i] represents the cost of the i-th fruit. Here's the twist: even if you can take a fruit for free, you can still choose to purchase it to unlock a new offer for subsequent fruits.

Goal: Find the minimum number of coins needed to acquire all fruits from the market.

Example: If prices = [3, 1, 2], buying fruit 1 for 3 coins gives you fruits 2 and 3 for free, costing only 3 coins total. Alternatively, buying fruit 2 for 1 coin gives you fruit 3 for free, then you'd need 3 coins for fruit 1, totaling 4 coins. The optimal choice is 3 coins!

Input & Output

example_1.py โ€” Basic Case
$ Input: prices = [3, 1, 2]
โ€บ Output: 4
๐Ÿ’ก Note: We can buy fruit 2 for 1 coin (get fruit 3 free), then buy fruit 1 for 3 coins. Total = 4 coins. Alternatively, buying fruit 1 first for 3 coins gets fruits 2 and 3 free, but costs more initially.
example_2.py โ€” Single Fruit
$ Input: prices = [1]
โ€บ Output: 1
๐Ÿ’ก Note: Only one fruit, must buy it for 1 coin.
example_3.py โ€” Optimal Strategy
$ Input: prices = [26, 18, 6, 12, 49, 7, 45, 45]
โ€บ Output: 39
๐Ÿ’ก Note: Optimal strategy involves buying certain fruits to maximize the free fruit coverage while minimizing total cost.

Visualization

Tap to expand
๐ŸŽ Fruit Market StrategyFruit 1Price: 3Get 1 freeFruit 2Price: 1Get 2 freeFruit 3Price: 2Get 3 freeFree!Free!Dynamic Programming Strategy1. Work backwards from last fruit2. For each fruit, calculate: min(buy_current + dp[next_uncovered], use_previous_deal)3. Maintain monotonic deque for efficient range minimum queries4. Deque stores indices of positions with increasing dp values5. Time: O(n), Space: O(n)Min Cost4
Understanding the Visualization
1
Analyze the Deals
Each fruit i offers next i fruits free when purchased
2
Work Backwards
Start from the end and determine optimal strategy for each position
3
Maintain Best Options
Use deque to track the cheapest ways to cover remaining fruits
4
Make Optimal Choice
At each fruit, choose between buying it or using a previous deal
Key Takeaway
๐ŸŽฏ Key Insight: The monotonic deque optimization allows us to efficiently find the minimum cost option among all valid previous purchases, reducing the time complexity from O(nยฒ) to O(n).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each element is added and removed from deque at most once, making it linear

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

DP array of size n plus deque storing at most n elements

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค prices.length โ‰ค 105
  • 1 โ‰ค prices[i] โ‰ค 109
  • The array is 1-indexed in the problem description but 0-indexed in implementation
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
43.5K Views
Medium Frequency
~35 min Avg. Time
1.8K 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