diff --git a/Dynamic_Programming/2431.Maximize-Total-Tastiness-of-Purchased-Fruits/2431.Maximize-Total-Tastiness-of-Purchased-Fruits.cpp b/Dynamic_Programming/2431.Maximize-Total-Tastiness-of-Purchased-Fruits/2431.Maximize-Total-Tastiness-of-Purchased-Fruits.cpp new file mode 100644 index 000000000..89ea96c2f --- /dev/null +++ b/Dynamic_Programming/2431.Maximize-Total-Tastiness-of-Purchased-Fruits/2431.Maximize-Total-Tastiness-of-Purchased-Fruits.cpp @@ -0,0 +1,36 @@ +class Solution { + int dp[1005][1005][6]; +public: + int maxTastiness(vector& price, vector& tastiness, int maxAmount, int maxCoupons) + { + int ret = 0; + dp[0][0][0] = 0; + if (price[0]<=maxamount) + { + dp[0][price[0]][0] = tastiness[0]; + ret = tastiness[0]; + } + if (price[0]/2<=maxamount && 1<=maxcoupons) + { + dp[0][price[0]/2][1] = tastiness[0]; + ret = tastiness[0]; + } + + for (int i=1; i=price[i]) + dp[i][j][k] = max(dp[i][j][k], dp[i-1][j-price[i]][k] + tastiness[i]); + if (j>=price[i]/2 && k>=1) + dp[i][j][k] = max(dp[i][j][k], dp[i-1][j-price[i]/2][k-1] + tastiness[i]); + + ret = max(ret, dp[i][j][k]); + } + + return ret; + + + } +}; diff --git a/Dynamic_Programming/2431.Maximize-Total-Tastiness-of-Purchased-Fruits/Readme.md b/Dynamic_Programming/2431.Maximize-Total-Tastiness-of-Purchased-Fruits/Readme.md new file mode 100644 index 000000000..a84b44348 --- /dev/null +++ b/Dynamic_Programming/2431.Maximize-Total-Tastiness-of-Purchased-Fruits/Readme.md @@ -0,0 +1,10 @@ +### 2431.Maximize-Total-Tastiness-of-Purchased-Fruits + +很常规的DP模式。令dp[i][j][k]表示前i个水果、花费j的钱、使用k张半价券,所能得到的最大tastiness。 + +显然,我们会考虑对第i个水果的决策: +1. 我们不买第i个水果,`dp[i][j][k] = dp[i-1][j][k]`; +2. 我们原价买第i个水果,`dp[i][j][k] = dp[i-1][j-price[i]][k] + tastiness[i]`; +3. 我们半价买第i个水果,`dp[i][j][k] = dp[i-1][j-price[i]/2][k-1] + tastiness[i]`; + +注意为了不出现越界,我们使用上述的转移方程时,需要对j和k加上约束。此外`i=0`时单独处理dp最为方便。 diff --git a/Readme.md b/Readme.md index 81de381d6..81d7523be 100644 --- a/Readme.md +++ b/Readme.md @@ -29,7 +29,6 @@ [1580.Put-Boxes-Into-the-Warehouse-II](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/1580.Put-Boxes-Into-the-Warehouse-II) (H-) [1687.Delivering-Boxes-from-Storage-to-Ports](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/1687.Delivering-Boxes-from-Storage-to-Ports) (H) [1793.Maximum-Score-of-a-Good-Subarray](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/1793.Maximum-Score-of-a-Good-Subarray) (M+) -[1798.Maximum-Number-of-Consecutive-Values-You-Can-Make](https://github.com/wisdompeak/LeetCode/blob/master/Greedy/1798.Maximum-Number-of-Consecutive-Values-You-Can-Make) (H-) [1989.Maximum-Number-of-People-That-Can-Be-Caught-in-Tag](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/1989.Maximum-Number-of-People-That-Can-Be-Caught-in-Tag) (M+) [2354.Number-of-Excellent-Pairs](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/2354.Number-of-Excellent-Pairs) (H-) [2422.Merge-Operations-to-Turn-Array-Into-a-Palindrome](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/2422.Merge-Operations-to-Turn-Array-Into-a-Palindrome) (H-) @@ -653,6 +652,7 @@ [2222.Number-of-Ways-to-Select-Buildings](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2222.Number-of-Ways-to-Select-Buildings) (M+) [2312.Selling-Pieces-of-Wood](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2312.Selling-Pieces-of-Wood) (M+) [2338.Count-the-Number-of-Ideal-Arrays](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2338.Count-the-Number-of-Ideal-Arrays) (H) +[2431.Maximize-Total-Tastiness-of-Purchased-Fruits](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2431.Maximize-Total-Tastiness-of-Purchased-Fruits) (M+) * ``基本型 I`` [198.House-Robber](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/198.House-Robber) (E) [213.House-Robber-II](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/213.House-Robber-II) (M+) @@ -1138,6 +1138,7 @@ [1727.Largest-Submatrix-With-Rearrangements](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1727.Largest-Submatrix-With-Rearrangements) (M) [1744.Can-You-Eat-Your-Favorite-Candy-on-Your-Favorite-Day](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1744.Can-You-Eat-Your-Favorite-Candy-on-Your-Favorite-Day) (M) [1788.Maximize-the-Beauty-of-the-Garden](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1788.Maximize-the-Beauty-of-the-Garden) (M+) +[1798.Maximum-Number-of-Consecutive-Values-You-Can-Make](https://github.com/wisdompeak/LeetCode/blob/master/Greedy/1798.Maximum-Number-of-Consecutive-Values-You-Can-Make) (H-) [1818.Minimum-Absolute-Sum-Difference](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1818.Minimum-Absolute-Sum-Difference) (M+) [1850.Minimum-Adjacent-Swaps-to-Reach-the-Kth-Smallest-Number](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1850.Minimum-Adjacent-Swaps-to-Reach-the-Kth-Smallest-Number) (M+) [1911.Maximum-Alternating-Subsequence-Sum](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1911.Maximum-Alternating-Subsequence-Sum) (M+)

AltStyle によって変換されたページ (->オリジナル) /