Watch 2 video solutions for Minimum Number of Coins for Fruits II, a hard level problem involving Array, Dynamic Programming, Queue. This walkthrough by Huifeng Guan has 253 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are at a fruit market with different types of exotic fruits on display.
You are given a 1-indexed array prices, where prices[i] denotes the number of coins needed to purchase the ith fruit.
The fruit market has the following offer:
ith fruit at prices[i] coins, you can get the next i fruits for free.Note that even if you can take fruit j for free, you can still purchase it for prices[j] coins to receive a new offer.
Return the minimum number of coins needed to acquire all the fruits.
Example 1:
Input: prices = [3,1,2] Output: 4 Explanation: You can acquire the fruits as follows: - Purchase the 1st fruit with 3 coins, and you are allowed to take the 2nd fruit for free. - Purchase the 2nd fruit with 1 coin, and you are allowed to take the 3rd fruit for free. - Take the 3rd fruit for free. Note that even though you were allowed to take the 2nd fruit for free, you purchased it because it is more optimal. It can be proven that 4 is the minimum number of coins needed to acquire all the fruits.
Example 2:
Input: prices = [1,10,1,1] Output: 2 Explanation: You can acquire the fruits as follows: - Purchase the 1st fruit with 1 coin, and you are allowed to take the 2nd fruit for free. - Take the 2nd fruit for free. - Purchase the 3rd fruit for 1 coin, and you are allowed to take the 4th fruit for free. - Take the 4th fruit for free. It can be proven that 2 is the minimum number of coins needed to acquire all the fruits.
Constraints:
1 <= prices.length <= 1051 <= prices[i] <= 105Problem Overview: You are given an array where price[i] is the cost of buying the i-th fruit. When you buy fruit i, you can take the next i fruits for free. The goal is to purchase fruits in a way that covers the entire array while minimizing the total number of coins spent.
Approach 1: Brute Force Dynamic Programming (O(n²) time, O(n) space)
Define dp[i] as the minimum cost required to obtain all fruits starting from index i. If you buy fruit i, you pay price[i] and can skip up to i fruits for free. The next paid purchase must happen in the range [i+1, i+i+1]. For every valid next position, compute price[i] + dp[next] and take the minimum. This approach directly follows the problem definition but scans a range of states for every index, leading to quadratic time.
Approach 2: DP with Min Heap (O(n log n) time, O(n) space)
The transition requires the minimum dp[j] in a sliding range of indices. Instead of scanning the range every time, maintain candidate states in a min heap. As you iterate from right to left, push valid dp values and remove entries that fall outside the allowed window [i+1, i+i+1]. The heap always exposes the smallest future cost in O(log n). This significantly improves performance for large inputs while keeping the implementation straightforward using a heap (priority queue).
Approach 3: Monotonic Queue Optimized DP (O(n) time, O(n) space)
The optimal solution observes that the heap is only used to track the minimum dp value in a sliding window. A monotonic queue maintains candidate indices with increasing dp values, allowing constant-time retrieval of the minimum. While iterating backward through the array, remove indices that fall outside the valid purchase window and maintain monotonic order by popping larger dp values from the back. The front of the deque always stores the best next state, so dp[i] = price[i] + dp[deque.front]. Each index enters and leaves the deque once, giving linear time.
Recommended for interviews: Start with the DP recurrence to show understanding of the purchase rule and state transition. Then optimize the range minimum lookup. Interviewers typically expect the monotonic queue solution because it reduces the transition from O(n) to O(1), achieving O(n) overall while demonstrating strong knowledge of dynamic programming optimization techniques.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Dynamic Programming | O(n²) | O(n) | Useful for understanding the state transition and verifying correctness on small inputs |
| DP with Min Heap | O(n log n) | O(n) | When you want a simpler optimization using a priority queue |
| DP with Monotonic Queue | O(n) | O(n) | Optimal solution for large arrays where sliding window minimum is required |