|
| 1 | +# [2969. Minimum Number of Coins for Fruits II](https://leetcode.cn/problems/minimum-number-of-coins-for-fruits-ii) |
| 2 | + |
| 3 | +[English Version](/solution/2900-2999/2969.Minimum%20Number%20of%20Coins%20for%20Fruits%20II/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>You are at a fruit market with different types of exotic fruits on display.</p> |
| 10 | + |
| 11 | +<p>You are given a <strong>1-indexed</strong> array <code>prices</code>, where <code>prices[i]</code> denotes the number of coins needed to purchase the <code>i<sup>th</sup></code> fruit.</p> |
| 12 | + |
| 13 | +<p>The fruit market has the following offer:</p> |
| 14 | + |
| 15 | +<ul> |
| 16 | + <li>If you purchase the <code>i<sup>th</sup></code> fruit at <code>prices[i]</code> coins, you can get the next <code>i</code> fruits for free.</li> |
| 17 | +</ul> |
| 18 | + |
| 19 | +<p><strong>Note</strong> that even if you <strong>can</strong> take fruit <code>j</code> for free, you can still purchase it for <code>prices[j]</code> coins to receive a new offer.</p> |
| 20 | + |
| 21 | +<p>Return <em>the <strong>minimum</strong> number of coins needed to acquire all the fruits</em>.</p> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | +<p><strong class="example">Example 1:</strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input:</strong> prices = [3,1,2] |
| 28 | +<strong>Output:</strong> 4 |
| 29 | +<strong>Explanation:</strong> You can acquire the fruits as follows: |
| 30 | +- Purchase the 1<sup>st</sup> fruit with 3 coins, and you are allowed to take the 2<sup>nd</sup> fruit for free. |
| 31 | +- Purchase the 2<sup>nd</sup> fruit with 1 coin, and you are allowed to take the 3<sup>rd</sup> fruit for free. |
| 32 | +- Take the 3<sup>rd</sup> fruit for free. |
| 33 | +Note that even though you were allowed to take the 2<sup>nd</sup> fruit for free, you purchased it because it is more optimal. |
| 34 | +It can be proven that 4 is the minimum number of coins needed to acquire all the fruits. |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p><strong class="example">Example 2:</strong></p> |
| 38 | + |
| 39 | +<pre> |
| 40 | +<strong>Input:</strong> prices = [1,10,1,1] |
| 41 | +<strong>Output:</strong> 2 |
| 42 | +<strong>Explanation:</strong> You can acquire the fruits as follows: |
| 43 | +- Purchase the 1<sup>st</sup> fruit with 1 coin, and you are allowed to take the 2<sup>nd</sup> fruit for free. |
| 44 | +- Take the 2<sup>nd</sup> fruit for free. |
| 45 | +- Purchase the 3<sup>rd</sup> fruit for 1 coin, and you are allowed to take the 4<sup>th</sup> fruit for free. |
| 46 | +- Take the 4<sup>t</sup><sup>h</sup> fruit for free. |
| 47 | +It can be proven that 2 is the minimum number of coins needed to acquire all the fruits. |
| 48 | +</pre> |
| 49 | + |
| 50 | +<p> </p> |
| 51 | +<p><strong>Constraints:</strong></p> |
| 52 | + |
| 53 | +<ul> |
| 54 | + <li><code>1 <= prices.length <= 10<sup>5</sup></code></li> |
| 55 | + <li><code>1 <= prices[i] <= 10<sup>5</sup></code></li> |
| 56 | +</ul> |
| 57 | + |
| 58 | +## 解法 |
| 59 | + |
| 60 | +<!-- 这里可写通用的实现逻辑 --> |
| 61 | + |
| 62 | +<!-- tabs:start --> |
| 63 | + |
| 64 | +### **Python3** |
| 65 | + |
| 66 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 67 | + |
| 68 | +```python |
| 69 | + |
| 70 | +``` |
| 71 | + |
| 72 | +### **Java** |
| 73 | + |
| 74 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 75 | + |
| 76 | +```java |
| 77 | + |
| 78 | +``` |
| 79 | + |
| 80 | +### **C++** |
| 81 | + |
| 82 | +```cpp |
| 83 | + |
| 84 | +``` |
| 85 | + |
| 86 | +### **Go** |
| 87 | + |
| 88 | +```go |
| 89 | + |
| 90 | +``` |
| 91 | + |
| 92 | +### **...** |
| 93 | + |
| 94 | +``` |
| 95 | +
|
| 96 | +``` |
| 97 | + |
| 98 | +<!-- tabs:end --> |
0 commit comments