当前位置:网站首页>Code Casual Recording Notes_Dynamic Programming_416 Segmentation and Subsetting
Code Casual Recording Notes_Dynamic Programming_416 Segmentation and Subsetting
2022-08-03 22:50:00 【Erik_Won】
代码随想录笔记_动态规划
代码随想录二刷笔记记录
LC416.分割等和子集
题目
01背包
给你一个 只包含正整数 的 非空 数组 nums .请你判断是否可以将这个数组分割成两个子集,使得两个子集的元素和相等.
示例 1:
输入:nums = [1,5,11,5]
输出:true
解释:数组可以分割成 [1, 5, 5] 和 [11] .
示例 2:
输入:nums = [1,2,3,5]
输出:false
解释:数组不能分割成两个元素和相等的子集.
思路分析
思路:
Divide the array into two subsets,make subset A 和子集 B the sum is equal
-> Find the set appears sum/2 的Subset sum.can be divided into two equal subsets
加入01背包的概念
1.背包的容量 sum/2
2.The weight of the item that needs to be put in the backpack is the value of the elementnums[i],价值也是nums[i]
3.背包如果正好装满,Indicates that equal subsets were found
4.Every element in the backpack is non-repeatable
动态规划五部曲
1.确定dp数组及其下标含义
dp[j]:Represents the sum of one of the subsets that makes the two subsets equal.
简单地说就是(符合题意)Subset sum
2.确定递推公式
dp[j] = max(dp[j],dp[j-nums[i]] + nums[i])
The weight and value of the items in this question are both nums[i]
3.初始化
Known from knowledge of rolling arrays,numThe elements in are all positive integers,则dp初始化为0即可.
Only all are initialized to0,The maximum value can only be taken during derivation.
反之,如果 num The elements in have negative numbers,则dp中的非0Subscript elements need to be initialized to negative infinity.
4.遍历顺序
for(int i = 0;i < nums.length;i++){
//先遍历物品,That is, the array value
for(int j = target; j >= nums[i];j --){
//遍历背包容量
//注意,Each element can only be placed once,Hence the reverse order,Avoid replay
dp[j] = max(dp[j],dp[j - nums[i]] + nums[i]);
}
}
5.推演分析
以 [1,5,5,11] 为例
容量 j | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
元素1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
元素5 | 0 | 1 | 1 | 1 | 1 | 5 | 6 | 6 | 6 | 6 | 6 | 6 |
元素5 | 0 | 1 | 1 | 1 | 1 | 5 | 6 | 6 | 6 | 6 | 6 | 11 |
图示:
代码实现
完整代码实现
public boolean canPartition(int[] nums) {
if (null == nums || nums.length == 0) return false;
// num 求和
int sum = Arrays.stream(nums).sum();
// 如果 sum 为奇数,Indicates that no two identical subsets could be found
if (sum % 2 == 1) return false;
// 背包容量
int bagSize = sum /2;
// 初始化dp数组
int[] dp = new int[bagSize + 1];
for (int i = 0; i < nums.length; i++) {
// 遍历元素
for (int j = bagSize; j >= nums[i]; j--) {
// 遍历背包
dp[j] = Math.max(dp[j],dp[j - nums[i]] + nums[i]);
}
}
return bagSize == dp[bagSize];
}
边栏推荐
- 完全二叉树问题
- Testng监听器
- Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
- start with connect by 实现递归查询
- First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.
- ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset
- Adobe是什么?
- UVa 437 - The Tower of Babylon (White Book)
- Live Preview | Build Business Intelligence, Quickly Embrace Financial Digital Transformation
- UVa 1025 - A Spy in the Metro(白书)
猜你喜欢
电商秒杀系统
ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例
win10系统下yolov5-V6.1版本的tensorrt部署细节教程及bug修改
Gains double award | know micro easily won the "2021 China digital twin solution suppliers in excellence" "made in China's smart excellent recommended products" double award!
HCIP BGP实验报告
pikachu Over permission 越权
Click the icon in Canvas App to generate PDF and save it to Dataverse
LabVIEW code generation error 61056
嵌入式系统:GPIO
Conditional Statements for Shell Programming
随机推荐
[RYU] rest_router.py source code analysis
嵌入式系统:概述
Internet user account information management regulations come into effect today: must crack down on account trading and gray products
Shell编程的条件语句
log4j-slf4j-impl cannot be present with log4j-to-slf4j
2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
Summary bug 】 【 Elipse garbled solution project code in Chinese!
[N1CTF 2018]eating_cms
BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记
Walk the Maze BFS
Codeup brushing notes - simple simulation
重发布实验报告
navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication
October 2019 Twice SQL Injection
Click the icon in Canvas App to generate PDF and save it to Dataverse
First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.
override学习(父类和子类)
软件测试内卷严重,如何提升自己的竞争力呢?
FinClip,助长智能电视更多想象空间
Nine ways to teach you to read the file path in the resources directory