当前位置:网站首页>LeetCode_416_分割等和子集
LeetCode_416_分割等和子集
2022-07-29 10:45:00 【Fitz1318】
题目链接
题目描述
给你一个 只包含正整数 的 非空 数组 nums 。请你判断是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。
示例 1:
输入:nums = [1,5,11,5]
输出:true
解释:数组可以分割成 [1, 5, 5] 和 [11] 。
示例 2:
输入:nums = [1,2,3,5]
输出:false
解释:数组不能分割成两个元素和相等的子集。
提示:
1 <= nums.length <= 2001 <= nums[i] <= 100
解题思路
动态规划法
- 背包的体积是sum/2
- 背包里要放入的商品(集合里的元素)重量为元素的数值,价值也是元素的数值
- 背包如果正好装满,说明找到了总和为sum/2的子集
- 背包中每一个元素是不可重复放入的
动态规划五部曲
- 确定
dp数组及下标的含义dp[j]:容量为j的背包,最大可以凑成j的子集总和为dp[j]
- 确定递推公式
- 在01背包中,递推公式为
dp[j] = Math.max(dp[j],dp[j-weight[i] + value[i]]),dp[j] = Math.max(dp[j],dp[j-nums[i]] + nums[i])
- 在01背包中,递推公式为
dp数组初始化dp[0] = 0,其他下标dp[i] = 0
- 确定遍历顺序
- 遍历物品的for循环放在外层,遍历背包的for循环放在内层,且内层的for循环倒序遍历
- 举例推到
dp数组- 如果dp[j] == j,则说明集合中的子集总和正好可以凑成总和
j
- 如果dp[j] == j,则说明集合中的子集总和正好可以凑成总和
AC代码
class Solution {
public boolean canPartition(int[] nums) {
int sum = 0;
for (int i : nums) {
sum += i;
}
if (sum % 2 != 0) {
return false;
}
int bagWeight = sum / 2;
int[] dp = new int[bagWeight + 1];
dp[0] = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = bagWeight; j >= nums[i]; j--) {
dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]);
}
}
return dp[bagWeight] == bagWeight;
}
}
边栏推荐
- Drawing box and ellipse of WPF screenshot control (IV) "imitating wechat"
- [paper reading] i-bert: integer only Bert quantification
- 8.穿插-从架构设计到实践理解ThreadPoolExecutor线程池
- Ggdag draw DAG and cause and effect diagram
- 阿里架构师耗时一年整理的《Lucene高级文档》,吃透你也是大厂员工!
- Open source, compliance escort! 2022 open atom global open source summit open source compliance sub forum is about to open
- Factoextra: visual PCA of multivariate statistical methods
- Object storage
- LeetCode二叉树系列——144.二叉树的前序遍历
- R 语言 二分法与 牛顿迭代法计算中方程的根
猜你喜欢

Two MySQL tables with different codes (utf8, utf8mb4) are joined, resulting in index failure

Kunlunbase instruction manual (II) best practices for peer-to-peer deployment

Comprehensive and detailed SQL learning guide (MySQL direction)

Attachment of text of chenjie Report

Meeting OA project (V) -- meeting notice and feedback details

Data visualization design guide (information chart)

Discussion on the application of arcing smart electricity in elderly care institutions

Drawing box and ellipse of WPF screenshot control (IV) "imitating wechat"
![[paper reading] q-bert: Hessian based ultra low precision quantification of Bert](/img/2d/3b9691c16d89dff1a8ac79105172d4.png)
[paper reading] q-bert: Hessian based ultra low precision quantification of Bert

remap_ Use of table in impdp
随机推荐
Using Riemann sum to calculate approximate integral in R language
Easy to understand and explain the gradient descent method!
【Unity,C#】Character键盘输入转向与旋转
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
Add: create Ou structure using PowerShell
A tour of grp:04 - GRP unary call unary call
Discussion on the application of arcing smart electricity in elderly care institutions
The 2022 open atom global open source summit opened in Beijing
js两个数组对象进行合并去重
[semantic segmentation] 2021-pvt iccv
周鸿祎:360是世界上最大的安全大数据公司
Evolution of xxl-job architecture for distributed scheduling
ECCV 2022 | CMU proposes to recurse on the visual transformer without adding parameters, and the amount of calculation is still small
Error: Protobuf syntax version should be first thing in file
Docker installation, redis configuration and remote connection
重磅 | 2022 开放原子全球开源峰会在北京开幕
会议OA项目(五)---- 会议通知、反馈详情
2022cuda summer training camp Day6 practice
matplotlib中文问题
factoextra:多元统计方法的可视化PCA