Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 4 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
int dp[1005][1005][6];
public:
int maxTastiness(vector<int>& price, vector<int>& 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.size(); i++)
for (int j=0; j<=maxAmount; j++)
for (int k=0; k<=maxCoupons; k++)
{
dp[i][j][k] = dp[i-1][j][k];
if (j>=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;


}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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最为方便。
3 changes: 2 additions & 1 deletion Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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-)
Expand Down Expand Up @@ -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+)
Expand Down Expand Up @@ -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+)
Expand Down

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