2944. Minimum Number of Coins for Fruits
Description
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)th
fruit atprices[i]
coins, you can get any number of the nexti
fruits 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 1st fruit with
prices[0] = 3
coins, you are allowed to take the 2nd fruit for free. - Purchase the 2nd fruit with
prices[1] = 1
coin, you are allowed to take the 3rd fruit for free. - Take the 3rd fruit for free.
Note that even though you could take the 2nd fruit for free as a reward of buying 1st 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 1st fruit with
prices[0] = 1
coin, you are allowed to take the 2nd fruit for free. - Take the 2nd fruit for free.
- Purchase the 3rd fruit for
prices[2] = 1
coin, you are allowed to take the 4th fruit for free. - Take the 4th fruit for free.
Example 3:
Input: prices = [26,18,6,12,49,7,45,45]
Output: 39
Explanation:
- Purchase the 1st fruit with
prices[0] = 26
coin, you are allowed to take the 2nd fruit for free. - Take the 2nd fruit for free.
- Purchase the 3rd fruit for
prices[2] = 6
coin, you are allowed to take the 4th, 5th and 6th (the next three) fruits for free. - Take the 4th fruit for free.
- Take the 5th fruit for free.
- Purchase the 6th fruit with
prices[5] = 7
coin, you are allowed to take the 8th and 9th fruit for free. - Take the 7th fruit for free.
- Take the 8th fruit for free.
Note that even though you could take the 6th fruit for free as a reward of buying 3rd fruit, you purchase it to receive its reward, which is more optimal.
Constraints:
1 <= prices.length <= 1000
1 <= prices[i] <= 105
Solutions
Solution 1: Memoization Search
We define a function \(\textit{dfs}(i)\) to represent the minimum number of coins needed to buy all the fruits starting from the \(i\)-th fruit. The answer is \(\textit{dfs}(1)\).
The execution logic of the function \(\textit{dfs}(i)\) is as follows:
- If \(i \times 2 \geq n\), it means that buying the \((i - 1)\)-th fruit is sufficient, and the remaining fruits can be obtained for free, so return \(\textit{prices}[i - 1]\).
- Otherwise, we can buy fruit \(i\), and then choose a fruit \(j\) to start buying from the next \(i + 1\) to \(2i + 1\) fruits. Thus, \(\textit{dfs}(i) = \textit{prices}[i - 1] + \min_{i + 1 \le j \le 2i + 1} \textit{dfs}(j)\).
To avoid redundant calculations, we use memoization to store the results that have already been computed. When encountering the same situation again, we directly return the result.
The time complexity is \(O(n^2)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(\textit{prices}\).
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Solution 2: Dynamic Programming
We can rewrite the memoization search in Solution 1 into a dynamic programming form.
Similar to Solution 1, we define \(f[i]\) to represent the minimum number of coins needed to buy all the fruits starting from the \(i\)-th fruit. The answer is \(f[1]\).
The state transition equation is \(f[i] = \min_{i + 1 \le j \le 2i + 1} f[j] + \textit{prices}[i - 1]\).
The time complexity is \(O(n^2)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(\textit{prices}\).
In the code implementation, we can directly use the \(\textit{prices}\) array to store the \(f\) array, thus optimizing the space complexity to \(O(1)\).
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 6 |
|
Solution 3: Dynamic Programming + Monotonic Queue Optimization
Observing the state transition equation in Solution 2, we can see that for each \(i\), we need to find the minimum value of \(f[i + 1], f[i + 2], \cdots, f[2i + 1]\). As \(i\) decreases, the range of these values also decreases. This is essentially finding the minimum value in a sliding window with a narrowing range, which can be optimized using a monotonic queue.
We calculate from back to front, maintaining a monotonically increasing queue \(q\), where the queue stores indices. If the front element of \(q\) is greater than \(i \times 2 + 1\), it means that the elements after \(i\) will not be used, so we dequeue the front element. If \(i\) is not greater than \((n - 1) / 2\), we can add \(\textit{prices}[q[0] - 1]\) to \(\textit{prices}[i - 1]\), and then add \(i\) to the back of the queue. If the fruit price corresponding to the back element of \(q\) is greater than or equal to \(\textit{prices}[i - 1]\), we dequeue the back element until the fruit price corresponding to the back element is less than \(\textit{prices}[i - 1]\) or the queue is empty, then add \(i\) to the back of the queue.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(\textit{prices}\).
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|