首页 > 其他分享 >day30打卡

day30打卡

时间:2024-10-13 10:43:37浏览次数:6  
标签:stones target int sum day30 vector 打卡 dp

1049 最后一块石头的重量II
class Solution {
public:
int lastStoneWeightII(vector& stones) {
int sum = 0;
for(int &it : stones)
{
sum += it;
}
int target = sum / 2;
vector dp(1501, 0);
for(int i = 0; i < stones.size(); ++i)
{
for(int j = target; j >= stones[i]; --j)
{
dp[j] = max(dp[j], dp[j-stones[i]] + stones[i]);
}
}
return sum - dp[target] - dp[target];
}
};

494 目标和

class Solution {
public:
int findTargetSumWays(vector& nums, int target) {
int sum = 0;
for(int &it : nums)
{
sum += it;
}
if (abs(target) > sum) return 0; // 此时没有方案
if ((target + sum) % 2 == 1) return 0;
int left = (target + sum) / 2;
vector dp(left + 1, 0);
dp[0] = 1;
for(int i = 0; i < nums.size(); ++i)
{
for(int j = left; j >= nums[i]; j--)
{
dp[j] += dp[j - nums[i]];
}
}
return dp[left];
}
};

474 一和零

class Solution {
public:
int findMaxForm(vector& strs, int m, int n) {
vector<vector> dp(m+1, vector(n+1, 0));
dp[0][0] = 0;
for(string &str : strs)
{
int x = 0;
int y = 0;
for(char &ch : str)
{
if(ch == '0')
{
++x;
}
else
{
++y;
}
}
for(int i = m; i >= x; --i)
{
for(int j = n; j >= y; --j)
{
dp[i][j] = max(dp[i][j], dp[i-x][j-y] + 1);
}
}
}
return dp[m][n];
}
};

标签:stones,target,int,sum,day30,vector,打卡,dp
From: https://www.cnblogs.com/pwangikun/p/18461969

相关文章