2.11-背包问题

ops/2025/2/12 21:33:08/

Code-2.11-背包问题

leetcode.cn/problems/NUPfPr/" rel="nofollow">LCR 101. 分割等和子集

分析:经典背包问题。

class Solution {
public:bool canPartition(vector<int>& nums) {int n = nums.size();int sum = 0;for (auto e : nums) sum += e;if (sum % 2 == 1) return false;int half = sum / 2;vector<vector<bool>> dp(n + 1, vector<bool>(half + 1));for (int i = 0; i < n + 1; i++) dp[i][0] = true;for (int i = 1; i < n + 1; i++)for (int j = 1; j < half + 1; j++) {dp[i][j] = dp[i - 1][j];if (j >= nums[i - 1]) {dp[i][j] = dp[i][j] || dp[i - 1][j - nums[i - 1]];}}return dp[n][half];}
};

分析:还可用滚动数组优化。

class Solution {
public:bool canPartition(vector<int>& nums) {int n = nums.size();int sum = 0;for (auto e : nums) sum += e;if (sum % 2 == 1) return false;int half = sum / 2;vector<bool> dp(half + 1);dp[0] = true;for (int i = 1; i < n + 1; i++)for (int j = half; j > 0 && j >= nums[i - 1]; j--)dp[j] = dp[j] || dp[j - nums[i - 1]];return dp[half];}
};

leetcode.cn/problems/YaVDxD/" rel="nofollow">LCR 102. 目标和

class Solution {
public:int findTargetSumWays(vector<int>& nums, int target) {int sum = 0;for (auto e : nums) sum += e;if ((sum - target) % 2 == 1 || sum - target < 0) return 0;int aim = (sum - target) / 2;vector<int> dp(aim + 1);dp[0] = 1;for (int i = 1; i < nums.size() + 1; i++)for (int j = aim; j >= nums[i - 1]; j--)dp[j] += dp[j - nums[i - 1]];return dp[aim];}
};

leetcode.cn/problems/last-stone-weight-ii/" rel="nofollow">1049. 最后一块石头的重量 II

分析:先把问题转化为背包问题,即:先用bool的dp数组,找到将石头分为两份的所有可能中最接近五五开的,然后再遍历dp即可。

class Solution {
public:int lastStoneWeightII(vector<int>& stones) {int sum = 0;for (auto e : stones) sum += e;int m = sum / 2;vector<bool> dp(m + 1);dp[0] = true;for (auto e : stones) {for (int j = m; j >= e; j--) {dp[j] = dp[j] || dp[j - e];}}for (int i = m;; i--) if (dp[i]) return sum - 2 * i;}
};

leetcode.cn/problems/gaM7Ch/" rel="nofollow">LCR 103. 零钱兑换

分析:从本题开始属于完全背包问题。

class Solution {
public:int coinChange(vector<int>& coins, int amount) {vector<int> dp(amount + 1, -1);dp[0] = 0;for (int e : coins) {for (int j = e; j < amount + 1; j++) {if (dp[j - e] != -1) {if (dp[j] == -1) {dp[j] = dp[j - e] + 1;} else {dp[j] = min(dp[j], dp[j - e] + 1);}}}}return dp[amount];}
};	

leetcode.cn/problems/coin-change-ii/" rel="nofollow">518. 零钱兑换 II

分析:这个题的数据中间会爆int,只有用unsigned long long才能过。

class Solution {
public:int change(int amount, vector<int>& coins) {vector<unsigned long long> dp(amount + 1, 0);dp[0] = 1;for (auto e : coins)for (unsigned long long j = 1; j < amount + 1; ++j) {if (j >= e) {dp[j] += dp[j - e];}}return (int)dp[amount];}
};

leetcode.cn/problems/perfect-squares/" rel="nofollow">279. 完全平方数

分析:和零钱兑换如出一辙。

class Solution {
public:int numSquares(int n) {vector<int> dp(n + 1);for (int e = 1; e * e <= n; e++)for (int j = e * e; j < n + 1; j++) {if (dp[j] == 0) {dp[j] = dp[j - e * e] + 1;} else {dp[j] = min(dp[j], dp[j - e * e] + 1);}}return dp[n];}
};

leetcode.cn/problems/ones-and-zeroes/" rel="nofollow">474. 一和零

分析:本题与下一题属于二位费用的背包问题,同样也可以优化为二维滚动数组,这里就不优化了。

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

leetcode.cn/problems/profitable-schemes/" rel="nofollow">879. 盈利计划

class Solution {
public:int profitableSchemes(int n, int minProfit, vector<int>& group, vector<int>& profit) {const int MOD = 1e9 + 7;int len = group.size();vector<vector<vector<int>>> dp(len + 1, vector<vector<int>>(n + 1, vector<int>(minProfit + 1, 0)));for (int j = 0; j <= n; j++) dp[0][j][0] = 1;for (int i = 1; i <= len; ++i) {for (int j = 0; j <= n; ++j) {for (int k = 0; k <= minProfit; ++k) {dp[i][j][k] = dp[i - 1][j][k];if (j >= group[i - 1]) {dp[i][j][k] += dp[i - 1][j - group[i - 1]][max(0, k - profit[i - 1])];dp[i][j][k] %= MOD;}}}}return dp[len][n][minProfit] % MOD;}
};

leetcode.cn/problems/D0F0SV/" rel="nofollow">LCR 104. 组合总和 Ⅳ

分析:由于本题的物品存放需要考虑顺序,所以要先遍历背包再遍历物品。并且此题用int存储会爆。

class Solution {
public:int combinationSum4(vector<int>& nums, int target) {vector<unsigned long long> dp(target + 1, 0);dp[0] = 1;for (unsigned long long j = 1; j < target + 1; j++) {for (auto e : nums) {if (j >= e)dp[j] += dp[j - e];}}return (int)dp[target];}
};

http://www.ppmy.cn/ops/157867.html

相关文章

寒假集训思维训练6

A - Humidifier 1 首先要知道&#xff0c;题目是按时间线的顺序给出了每一次加水操作的 加湿器每分钟会消耗1升水 对于这种要记录时间线的题目&#xff0c;通常我们应该自己记录一个 T T T 一开始时间 T 0 &#xff0c;答案剩余水 a n s 0 T0&#xff0c;答案剩余水ans0 T0&a…

食品饮料生产瓶颈?富唯智能协作机器人来 “破壁”

在食品和饮料行业的发展进程中&#xff0c;诸多生产瓶颈如重复性劳动负担、复杂环境作业难题、季节性产能波动等&#xff0c;长期制约着企业的高效运营与进一步发展。如今&#xff0c;富唯智能协作机器人的出现&#xff0c;为这些难题提供了完美的解决方案&#xff0c;正逐步改…

ESP32S3基于espidf ADC使用

ESP32S3基于espidf ADC使用 官方在线文档介绍模数转换器&#xff1a;https://docs.espressif.com/projects/esp-idf/zh_CN/stable/esp32s3/api-reference/peripherals/adc_oneshot.html&#x1f516;espidf版本&#xff1a;v5.4 模数转换器 (ADC)转换方式&#xff1a; 模数转换…

【STM32系列】利用MATLAB配合ARM-DSP库设计FIR数字滤波器(保姆级教程)

ps.源码放在最后面 设计IIR数字滤波器可以看这里&#xff1a;利用MATLAB配合ARM-DSP库设计IIR数字滤波器&#xff08;保姆级教程&#xff09; 前言 本篇文章将介绍如何利用MATLAB与STM32的ARM-DSP库相结合&#xff0c;简明易懂地实现FIR低通滤波器的设计与应用。文章重点不在…

Android车机DIY开发之软件篇(十二) AOSP12下载编译

Android车机DIY开发之软件篇(十二) AOSP12下载编译 sudo apt-get update sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib gmultilib libc6-dev-i386 lib32ncurses5-dev libx11-dev lib32z-dev ccache libgl1-mesa-…

基于Python的人工智能驱动基因组变异算法:设计与应用(下)

3.3.2 数据清洗与预处理 在基因组变异分析中,原始数据往往包含各种噪声和不完整信息,数据清洗与预处理是确保分析结果准确性和可靠性的关键步骤。通过 Python 的相关库和工具,可以有效地去除噪声、填补缺失值、标准化数据等,为后续的分析提供高质量的数据基础。 在基因组…

Maven在idea中的使用

介绍 什么是maven&#xff1f; Maven是apache旗下的一个开源项目&#xff0c;是一款管理和构建java项目的工具。 maven作用&#xff1f; 方便快捷的管理项目依赖和资源&#xff08;jar包&#xff09;&#xff0c;避免版本冲突问题 。提供标准、统一的项目结构。标准跨平台&…

爬虫工程师分享:获取京东商品详情SKU数据的技术难点与攻破方法

在电商数据领域&#xff0c;京东商品详情页的SKU数据是许多爬虫工程师的目标。这些数据包含了商品的价格、库存、规格等关键信息&#xff0c;对于市场分析、价格监控等应用场景至关重要。然而&#xff0c;获取这些数据并非易事&#xff0c;京东作为国内电商巨头&#xff0c;其反…