|
| 1 | +# [2431. Maximize Total Tastiness of Purchased Fruits](https://leetcode.cn/problems/maximize-total-tastiness-of-purchased-fruits) |
| 2 | + |
| 3 | +[English Version](/solution/2400-2499/2431.Maximize%20Total%20Tastiness%20of%20Purchased%20Fruits/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>You are given two non-negative integer arrays <code>price</code> and <code>tastiness</code>, both arrays have the same length <code>n</code>. You are also given two non-negative integers <code>maxAmount</code> and <code>maxCoupons</code>.</p> |
| 10 | + |
| 11 | +<p>For every integer <code>i</code> in range <code>[0, n - 1]</code>:</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li><code>price[i]</code> describes the price of <code>i<sup>th</sup></code> fruit.</li> |
| 15 | + <li><code>tastiness[i]</code> describes the tastiness of <code>i<sup>th</sup></code> fruit.</li> |
| 16 | +</ul> |
| 17 | + |
| 18 | +<p>You want to purchase some fruits such that total tastiness is maximized and the total price does not exceed <code>maxAmount</code>.</p> |
| 19 | + |
| 20 | +<p>Additionally, you can use a coupon to purchase fruit for <strong>half of its price</strong> (rounded down to the closest integer). You can use at most <code>maxCoupons</code> of such coupons.</p> |
| 21 | + |
| 22 | +<p>Return <em>the maximum total tastiness that can be purchased</em>.</p> |
| 23 | + |
| 24 | +<p><strong>Note that:</strong></p> |
| 25 | + |
| 26 | +<ul> |
| 27 | + <li>You can purchase each fruit at most once.</li> |
| 28 | + <li>You can use coupons on some fruit at most once.</li> |
| 29 | +</ul> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | +<p><strong class="example">Example 1:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>Input:</strong> price = [10,20,20], tastiness = [5,8,8], maxAmount = 20, maxCoupons = 1 |
| 36 | +<strong>Output:</strong> 13 |
| 37 | +<strong>Explanation:</strong> It is possible to make total tastiness 13 in following way: |
| 38 | +- Buy first fruit without coupon, so that total price = 0 + 10 and total tastiness = 0 + 5. |
| 39 | +- Buy second fruit with coupon, so that total price = 10 + 10 and total tastiness = 5 + 8. |
| 40 | +- Do not buy third fruit, so that total price = 20 and total tastiness = 13. |
| 41 | +It can be proven that 13 is the maximum total tastiness that can be obtained. |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p><strong class="example">Example 2:</strong></p> |
| 45 | + |
| 46 | +<pre> |
| 47 | +<strong>Input:</strong> price = [10,15,7], tastiness = [5,8,20], maxAmount = 10, maxCoupons = 2 |
| 48 | +<strong>Output:</strong> 28 |
| 49 | +<strong>Explanation:</strong> It is possible to make total tastiness 20 in following way: |
| 50 | +- Do not buy first fruit, so that total price = 0 and total tastiness = 0. |
| 51 | +- Buy second fruit with coupon, so that total price = 0 + 7 and total tastiness = 0 + 8. |
| 52 | +- Buy third fruit with coupon, so that total price = 7 + 3 and total tastiness = 8 + 20. |
| 53 | +It can be proven that 28 is the maximum total tastiness that can be obtained. |
| 54 | +</pre> |
| 55 | + |
| 56 | +<p> </p> |
| 57 | +<p><strong>Constraints:</strong></p> |
| 58 | + |
| 59 | +<ul> |
| 60 | + <li><code>n == price.length == tastiness.length</code></li> |
| 61 | + <li><code>1 <= n <= 100</code></li> |
| 62 | + <li><code>0 <= price[i], tastiness[i], maxAmount <= 1000</code></li> |
| 63 | + <li><code>0 <= maxCoupons <= 5</code></li> |
| 64 | +</ul> |
| 65 | + |
| 66 | +## 解法 |
| 67 | + |
| 68 | +<!-- 这里可写通用的实现逻辑 --> |
| 69 | + |
| 70 | +**方法一:记忆化搜索** |
| 71 | + |
| 72 | +设计函数 $dfs(i, j, k)$ 表示从第 $i$ 个水果开始,剩余 $j$ 元钱,剩余 $k$ 张优惠券时,最大的总美味度。 |
| 73 | + |
| 74 | +对于第 $i$ 个水果,可以选择购买或者不购买,如果购买,那么可以选择使用优惠券或者不使用优惠券。 |
| 75 | + |
| 76 | +如果不购买,那么最大总美味度是 $dfs(i + 1, j, k)$; |
| 77 | + |
| 78 | +如果购买,如果不使用优惠券(需要满足 $j\ge price[i]$),那么最大总美味度是 $dfs(i + 1, j - price[i], k) + tastiness[i]$;如果使用优惠券(需要满足 $k\gt 0$ 并且 $j\ge \lfloor \frac{price[i]}{2} \rfloor$),那么最大总美味度是 $dfs(i + 1, j - \lfloor \frac{price[i]}{2} \rfloor, k - 1) + tastiness[i]$。 |
| 79 | + |
| 80 | +最终的答案是 $dfs(0, maxAmount, maxCoupons)$。 |
| 81 | + |
| 82 | +时间复杂度 $O(n \times maxAmount \times maxCoupons)$。其中 $n$ 是水果的数量。 |
| 83 | + |
| 84 | +<!-- tabs:start --> |
| 85 | + |
| 86 | +### **Python3** |
| 87 | + |
| 88 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 89 | + |
| 90 | +```python |
| 91 | +class Solution: |
| 92 | + def maxTastiness(self, price: List[int], tastiness: List[int], maxAmount: int, maxCoupons: int) -> int: |
| 93 | + @cache |
| 94 | + def dfs(i, j, k): |
| 95 | + if i == len(price): |
| 96 | + return 0 |
| 97 | + ans = dfs(i + 1, j, k) |
| 98 | + if j >= price[i]: |
| 99 | + ans = max(ans, dfs(i + 1, j - price[i], k) + tastiness[i]) |
| 100 | + if j >= price[i] // 2 and k: |
| 101 | + ans = max( |
| 102 | + ans, dfs(i + 1, j - price[i] // 2, k - 1) + tastiness[i]) |
| 103 | + return ans |
| 104 | + |
| 105 | + return dfs(0, maxAmount, maxCoupons) |
| 106 | +``` |
| 107 | + |
| 108 | +### **Java** |
| 109 | + |
| 110 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 111 | + |
| 112 | +```java |
| 113 | +class Solution { |
| 114 | + private int[][][] f; |
| 115 | + private int[] price; |
| 116 | + private int[] tastiness; |
| 117 | + private int n; |
| 118 | + |
| 119 | + public int maxTastiness(int[] price, int[] tastiness, int maxAmount, int maxCoupons) { |
| 120 | + n = price.length; |
| 121 | + this.price = price; |
| 122 | + this.tastiness = tastiness; |
| 123 | + f = new int[n][maxAmount + 1][maxCoupons + 1]; |
| 124 | + return dfs(0, maxAmount, maxCoupons); |
| 125 | + } |
| 126 | + |
| 127 | + private int dfs(int i, int j, int k) { |
| 128 | + if (i == n) { |
| 129 | + return 0; |
| 130 | + } |
| 131 | + if (f[i][j][k] != 0) { |
| 132 | + return f[i][j][k]; |
| 133 | + } |
| 134 | + int ans = dfs(i + 1, j, k); |
| 135 | + if (j >= price[i]) { |
| 136 | + ans = Math.max(ans, dfs(i + 1, j - price[i], k) + tastiness[i]); |
| 137 | + } |
| 138 | + if (j >= price[i] / 2 && k > 0) { |
| 139 | + ans = Math.max(ans, dfs(i + 1, j - price[i] / 2, k - 1) + tastiness[i]); |
| 140 | + } |
| 141 | + f[i][j][k] = ans; |
| 142 | + return ans; |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +### **C++** |
| 148 | + |
| 149 | +```cpp |
| 150 | +class Solution { |
| 151 | +public: |
| 152 | + int maxTastiness(vector<int>& price, vector<int>& tastiness, int maxAmount, int maxCoupons) { |
| 153 | + int n = price.size(); |
| 154 | + memset(f, 0, sizeof f); |
| 155 | + function<int(int i, int j, int k)> dfs; |
| 156 | + dfs = [&](int i, int j, int k) { |
| 157 | + if (i == n) return 0; |
| 158 | + if (f[i][j][k]) return f[i][j][k]; |
| 159 | + int ans = dfs(i + 1, j, k); |
| 160 | + if (j >= price[i]) ans = max(ans, dfs(i + 1, j - price[i], k) + tastiness[i]); |
| 161 | + if (j >= price[i] / 2 && k) ans = max(ans, dfs(i + 1, j - price[i] / 2, k - 1) + tastiness[i]); |
| 162 | + f[i][j][k] = ans; |
| 163 | + return ans; |
| 164 | + }; |
| 165 | + return dfs(0, maxAmount, maxCoupons); |
| 166 | + } |
| 167 | +private: |
| 168 | + int f[101][1001][6]; |
| 169 | +}; |
| 170 | +``` |
| 171 | +
|
| 172 | +### **Go** |
| 173 | +
|
| 174 | +```go |
| 175 | +func maxTastiness(price []int, tastiness []int, maxAmount int, maxCoupons int) int { |
| 176 | + n := len(price) |
| 177 | + f := make([][][]int, n+1) |
| 178 | + for i := range f { |
| 179 | + f[i] = make([][]int, maxAmount+1) |
| 180 | + for j := range f[i] { |
| 181 | + f[i][j] = make([]int, maxCoupons+1) |
| 182 | + } |
| 183 | + } |
| 184 | + var dfs func(i, j, k int) int |
| 185 | + dfs = func(i, j, k int) int { |
| 186 | + if i == n { |
| 187 | + return 0 |
| 188 | + } |
| 189 | + if f[i][j][k] != 0 { |
| 190 | + return f[i][j][k] |
| 191 | + } |
| 192 | + ans := dfs(i+1, j, k) |
| 193 | + if j >= price[i] { |
| 194 | + ans = max(ans, dfs(i+1, j-price[i], k)+tastiness[i]) |
| 195 | + } |
| 196 | + if j >= price[i]/2 && k > 0 { |
| 197 | + ans = max(ans, dfs(i+1, j-price[i]/2, k-1)+tastiness[i]) |
| 198 | + } |
| 199 | + f[i][j][k] = ans |
| 200 | + return ans |
| 201 | + } |
| 202 | + return dfs(0, maxAmount, maxCoupons) |
| 203 | +} |
| 204 | +
|
| 205 | +func max(a, b int) int { |
| 206 | + if a > b { |
| 207 | + return a |
| 208 | + } |
| 209 | + return b |
| 210 | +} |
| 211 | +``` |
| 212 | + |
| 213 | +### **TypeScript** |
| 214 | + |
| 215 | +```ts |
| 216 | + |
| 217 | +``` |
| 218 | + |
| 219 | +### **...** |
| 220 | + |
| 221 | +``` |
| 222 | +
|
| 223 | +``` |
| 224 | + |
| 225 | +<!-- tabs:end --> |
0 commit comments