Minimum Number of Coins for Fruits Problem
#2944
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 2944.
You are given an 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)^thfruit atprices[i]coins, you can get any number of the nextifruits for free.
Note that 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.
Example 1:
Input: prices = [3,1,2]
Output: 4
Explanation:
- Purchase the 1^st fruit with prices[0] = 3 coins, you are allowed to take the 2^nd fruit for free.
- Purchase the 2^nd fruit with prices[1] = 1 coin, you are allowed to take the 3^rd fruit for free.
- Take the 3^rd fruit for free.
Note that even though you could take the 2^nd fruit for free as a reward of buying 1^st fruit, you purchase it to receive its reward, which is more optimal.
Example 2:
Input: prices = [1,10,1,1]
Output: 2
Explanation:
- Purchase the 1^st fruit with prices[0] = 1 coin, you are allowed to take the 2^nd fruit for free.
- Take the 2^nd fruit for free.
- Purchase the 3^rd fruit for prices[2] = 1 coin, you are allowed to take the 4^th fruit for free.
- Take the 4^th fruit for free.
Example 3:
Input: prices = [26,18,6,12,49,7,45,45]
Output: 39
Explanation:
- Purchase the 1^st fruit with prices[0] = 26 coin, you are allowed to take the 2^nd fruit for free.
- Take the 2^nd fruit for free.
- Purchase the 3^rd fruit for prices[2] = 6 coin, you are allowed to take the 4^th, 5^th and 6^th (the next three) fruits for free.
- Take the 4^th fruit for free.
- Take the 5^th fruit for free.
- Purchase the 6^th fruit with prices[5] = 7 coin, you are allowed to take the 8^th and 9^th fruit for free.
- Take the 7^th fruit for free.
- Take the 8^th fruit for free.
Note that even though you could take the 6^th fruit for free as a reward of buying 3^rd fruit, you purchase it to receive its reward, which is more optimal.
Constraints:
1 <= prices.length <= 10001 <= prices[i] <= 10^5
Solution
Approach: Memoized Search
Let dfs(i) be the minimum cost to get all fruits from the i-th one onward, assuming we buy fruit i. The answer is dfs(1) since fruit 1 has to be bought.
- If
2i >= n, buying fruitialready covers every remaining fruit, so the cost isprices[i - 1]. - Otherwise fruit
icovers fruitsi + 1to2i, so the next fruit we buy is somejbetweeni + 1and2i + 1. The cost isprices[i - 1] + min(dfs(j))over thosej.
Store each result so it is computed once.
Complexity: O(n2) time and O(n) space, where n is the number of fruits.
from functools import lru_cache
class Solution:
def minimumCoins(self, prices: List[int]) -> int:
@lru_cache(None)
def dfs(i: int) -> int:
if i * 2 >= len(prices):
return prices[i - 1]
return prices[i - 1] + min(dfs(j) for j in range(i + 1, i * 2 + 2))
return dfs(1)