1049. 最后一块石头的重量 II https://leetcode.cn/problems/last-stone-weight-ii
1049. Last Stone Weight II https://leetcode.com/problems/last-stone-weight-ii
474. 一和零 https://leetcode.cn/problems/ones-and-zeroes
474. Ones and Zeroes https://leetcode.com/problems/ones-and-zeroes
3180. 执行操作可获得的最大总奖励 I https://leetcode.cn/problems/maximum-total-reward-using-operations-i
3180. Maximum Total Reward Using Operations I https://leetcode.com/problems/maximum-total-reward-using-operations-i
背包问题是一类经典的动态规划问题,主要是在某个限制内,选择最优的元素组合。
其特点在于 dp
数组的定义方式,第一个维度的定义是「仅使用前 i
个物品」,之后的维度用来定义限制条件。
比如经典的 0-1 背包问题,dp
数组定义如下:
// 定义:dp[i][j] 表示使用前 i 个物品,背包容量为 j 的情况下,可以获得的最大价值
int[][] dp = new int[N + 1][W + 1];
下面列举几道和背包问题相关的题目,帮助大家加深理解。