0% completed
You are given an array of integers prices
where each element prices[i]
represents the number of coins required to buy the i<sup>th</sup> fruit.
The fruit market has the following reward for each fruit:
prices[i]
coins, you can get the next (i + 1)
fruits for free.Note: Even if you have the option to take certain fruits for free, you can still choose to purchase them at their respective prices[j]
to gain the reward for the next fruits.
Return the minimum
number of coins needed to get all the fruits.
i + 1 = 0 + 1 = 1
fruit for free.i + 1 = 2 + 1 = 3
fruits for free.3
coins to get the third and fourth fruit for free.i + 1 = 0 + 1 = 1
fruit for free.i + 1 = 2 + 1 = 3
fruits for free.Constraints:
To solve the problem of finding the minimum number of coins needed to purchase all the fruits, we use a technique that involves iterating through the fruit prices from the last one to the first. By doing this, we can determine the most cost-effective way to buy each fruit and any subsequent fruits that might be available for free as a reward. We keep track of the best options using a deque (a double-ended queue) that helps us efficiently manage which fruits to consider for minimizing the total cost.
The deque is used to store the indices of the fruits in a way that allows us to quickly find the minimum cost for any given range of fruits. As we iterate through the prices, we update each fruit’s cost to include the cheapest possible way to get all the subsequent fruits. By the end of the loop, the total minimum cost to purchase all fruits is stored in the first element of the prices array, which is then returned as the result. This approach is both efficient and straightforward, ensuring we achieve the optimal solution.
Initialize Variables:
Extend Prices Array:
0
because no additional cost is required after the last fruit.Start Iteration:
Calculate Maximum Covered Index:
Manage the Deque:
Add Current Fruit Index to Deque:
Return the Minimum Coins:
Let's walk through the algorithm step by step with the input prices = [1, 6, 1, 2, 4].
Initial Setup:
prices = [1, 6, 1, 2, 4]
and deque = []
.0
to prices
to avoid boundary issues, so prices = [1, 6, 1, 2, 4, 0]
.n = 5
to deque
, so deque = [5]
.Iteration 1 (i = 4
):
maxCoveredIndex = Math.min(5, 2 * 4 + 2) = 5
.deque
since deque[0] = 5
is within range.prices[4]
: prices[4] = 4 + prices[5] = 4 + 0 = 4
.deque
since prices[i] < prices[deque[deque.length - 1]] (4 < 0)
is not true.4
to deque
, so deque = [5, 4]
.Iteration 2 (i = 3
):
maxCoveredIndex = Math.min(5, 2 * 3 + 2) = 5
.deque
since deque[0] = 5
is within range.prices[3]
: prices[3] = 2 + prices[5] = 2 + 0 = 6
.4
from deque
as prices[3] < prices[4]
.3
to deque
, so deque = [5, 3]
.Iteration 3 (i = 2
):
maxCoveredIndex = Math.min(5, 2 * 2 + 2) = 5
.deque
since deque[0] = 5
is within range.prices[2]
: prices[2] = 1 + prices[5] = 1 + 0 = 1
.3
from deque
as prices[2] < prices[3]
.2
to deque
, so deque = [5, 2]
.Iteration 4 (i = 1
):
maxCoveredIndex = Math.min(5, 2 * 1 + 2) = 4
.5
from deque
since deque[0] = 5
is out of range. Now, deque = [2]prices[1]
: prices[1] = 6 + prices[2] = 6 + 1 = 7
.1
to deque
, so deque = [2, 1]
.Iteration 5 (i = 0
):
maxCoveredIndex = Math.min(5, 2 * 0 + 2) = 2
.deque
since deque[0] = 2
is within range.prices[0]
: prices[0] = 1 + prices[2] = 1 + 1 = 2
.0
to deque
, so deque = [2, 0]
.Final Output:
prices[0]
is 2
, which is the minimum cost to buy all fruits.prices
array exactly once, which is O(n).Overall, the time complexity of the algorithm is O(n), where n
is the number of elements in the prices
array.
prices
array, but this is also O(n).Overall, the space complexity of the algorithm is O(n).