Skip to content

Commit 31b4fd9

Browse files
committed
feat: add solutions to lc problem: No.2431
No.2431.Maximize Total Tastiness of Purchased Fruits
1 parent de189d3 commit 31b4fd9

File tree

14 files changed

+571
-36
lines changed

14 files changed

+571
-36
lines changed

solution/0100-0199/0160.Intersection of Two Linked Lists/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
<p><strong>示例 1:</strong></p>
3636

37-
<p><a href="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_1.png" target="_blank"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_1_1.png" style="height:130px; width:400px" /></a></p>
37+
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_1.png" target="_blank"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_1_1.png" style="height:130px; width:400px" /></a></p>
3838

3939
<pre>
4040
<strong>输入:</strong>intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
@@ -49,7 +49,7 @@
4949

5050
<p><strong>示例&nbsp;2:</strong></p>
5151

52-
<p><a href="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_2.png" target="_blank"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_2.png" style="height:136px; width:350px" /></a></p>
52+
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_2.png" target="_blank"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_2.png" style="height:136px; width:350px" /></a></p>
5353

5454
<pre>
5555
<strong>输入:</strong>intersectVal&nbsp;= 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
@@ -61,7 +61,7 @@
6161

6262
<p><strong>示例&nbsp;3:</strong></p>
6363

64-
<p><a href="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_3.png" target="_blank"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_3.png" style="height:126px; width:200px" /></a></p>
64+
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_3.png" target="_blank"><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_3.png" style="height:126px; width:200px" /></a></p>
6565

6666
<pre>
6767
<strong>输入:</strong>intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2

solution/2400-2499/2422.Merge Operations to Turn Array Into a Palindrome/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
<ul>
1414
<li>Choose any two <strong>adjacent</strong> elements and <strong>replace</strong> them with their <strong>sum</strong>.
15-
<ul>
16-
<li>For example, if <code>nums = [1,<u>2,3</u>,1]</code>, you can apply one operation to make it <code>[1,5,1]</code>.</li>
17-
</ul>
18-
</li>
15+
<ul>
16+
<li>For example, if <code>nums = [1,<u>2,3</u>,1]</code>, you can apply one operation to make it <code>[1,5,1]</code>.</li>
17+
</ul>
18+
</li>
1919
</ul>
2020

2121
<p>Return <em>the <strong>minimum</strong> number of operations needed to turn the array into a <strong>palindrome</strong></em>.</p>

solution/2400-2499/2422.Merge Operations to Turn Array Into a Palindrome/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
<ul>
1212
<li>Choose any two <strong>adjacent</strong> elements and <strong>replace</strong> them with their <strong>sum</strong>.
13-
<ul>
14-
<li>For example, if <code>nums = [1,<u>2,3</u>,1]</code>, you can apply one operation to make it <code>[1,5,1]</code>.</li>
15-
</ul>
16-
</li>
13+
<ul>
14+
<li>For example, if <code>nums = [1,<u>2,3</u>,1]</code>, you can apply one operation to make it <code>[1,5,1]</code>.</li>
15+
</ul>
16+
</li>
1717
</ul>
1818

1919
<p>Return <em>the <strong>minimum</strong> number of operations needed to turn the array into a <strong>palindrome</strong></em>.</p>

solution/2400-2499/2424.Longest Uploaded Prefix/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ server.longest(); // The prefix [1,2,3] is the longest upload
4242

4343
<ul>
4444
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
45-
<li><code>1 &lt;= video &lt;= 10<sup>5</sup></code></li>
45+
<li><code>1 &lt;= video &lt;= n</code></li>
4646
<li>All values of <code>video</code> are <strong>distinct</strong>.</li>
4747
<li>At most <code>2 * 10<sup>5</sup></code> calls <strong>in total</strong> will be made to <code>upload</code> and <code>longest</code>.</li>
4848
<li>At least one call will be made to <code>longest</code>.</li>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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>&nbsp;</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>&nbsp;</p>
57+
<p><strong>Constraints:</strong></p>
58+
59+
<ul>
60+
<li><code>n == price.length == tastiness.length</code></li>
61+
<li><code>1 &lt;= n &lt;= 100</code></li>
62+
<li><code>0 &lt;= price[i], tastiness[i], maxAmount &lt;= 1000</code></li>
63+
<li><code>0 &lt;= maxCoupons &lt;= 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

Comments
 (0)