当前位置:网站首页>Leetode 416. Divide equal sum subsets
Leetode 416. Divide equal sum subsets
2022-07-22 06:11:00 【Code of attack】
416. To divide into equal and subsets
List of articles
One 、 subject
Original link :416. To divide into equal and subsets
1. Title Description
To give you one Contains only positive integers Of Non empty Array nums
. Please decide whether you can divide this array into two subsets , Make the sum of the elements of the two subsets equal .
Example 1:
Input :nums = [1,5,11,5]
Output :true
explain : Arrays can be divided into [1, 5, 5] and [11] .
Example 2:
Input :nums = [1,2,3,5]
Output :false
explain : An array cannot be divided into two elements and equal subsets .
Tips :
1 <= nums.length <= 200
1 <= nums[i] <= 100
2. Basic framework
C++ The basic framework code is as follows :
bool canPartition(vector<int>& nums) {
}
3. Their thinking
- Topic analysis
- The goal of the question is to determine whether the array can be divided into two subsets , It can be for true, Otherwise false.
- The elements in these two subsets can be discontinuous , So we can't solve the problem by prefix and .
- We have to try our best to prove that it has two subsets of elements and is equal .
- First of all, will nums Sum the elements of the array , by
sum
. As long as it is proved that the sum of elements in the set issum / 2
The combination of , It means nums An array can be divided into two elements and equal subsets . - If
sum % 2 == 1
, Note that there are no two elements and the same subset , return false. - Each element can only be taken once .
- Come here , You will find , Follow 01 The solution of knapsack problem is the same , Each element value is equivalent to 01 Items in the backpack , And can only be taken once ,01 The capacity in the backpack is target, Here it is
sum / 2
, Put these two elements into 01 Just solve the knapsack formula .
Implementation code :
bool canPartition(vector<int>& nums) { int sum = 0; for (int i = 0; i < nums.size(); i++) sum += nums[i]; if (sum % 2 == 1) return false; int target = sum / 2; // amount to 01 Capacity in backpack memset(dp, 0, sizeof(dp)); for (int i = 0; i < nums.size(); i++) for (int j = target; j >= nums[i]; j--) dp[j] = max(dp[j], dp[ j - nums[i] ] + nums[i]); if (target == dp[target]) // This indicates that the sum of the elements in the set is target return true; return false; }
边栏推荐
- 申万宏源证券股票低佣金开户靠谱吗,可靠安全吗
- Development of digital collection system -- Construction of mall blind box H5 platform
- Leetode 416.分割等和子集
- Zabbix3.0版报警设置
- Early details about visual studio 2019
- 微信小程序_19,自定义组件-behaviors
- [book club issue 13] + Chapter 1 multimedia processing tool ffmpeg tool set
- 机房可视化管理标签自动生成
- 剑指 Offer II 015. 字符串中的所有变位词
- DETR代码
猜你喜欢
数据中心线缆管理
微信小程序_19,自定义组件-behaviors
The pit trodden by real people tells you to avoid the 10 mistakes that novices in automated testing often make
leetcode 931.下降路径最小和
DLL免杀技术探讨
An idea of solving agile iteration of desktop application with applet Technology
Netcore——Middleware中间件(1)
机房可视化管理标签自动生成
Discussion on DLL killing free technology
The role of visual management of communication infrastructure in ISO 2.0
随机推荐
关于Visual Studio 2019的前期详情
Typescript learning
AUTOCAD——JOIN合并命令
2022长三角工业自动化展会将于10月在南京国际展览中心召开
supervisor常用命令
leetcode 376.摆动序列
A MySQL misoperation led to a P0 level accident
数字藏品系统开发——商城盲盒h5平台搭建
iNFTnews | 佳士得推出风险投资部门,瞄准Web3和元宇宙产业
2022年数据库审计产品排行榜-必看!
Internet and transport layer protocol
一些工具改造
A device from black to white (with front desk 0day)
Those violations in the store will be punished by the official secondary punishment, the most common four
D3.js + canvas drawing organization chart
Kali WiFi cracking (multiple ways)
The development trend of the meta universe has been supported, and the era that everything can be a meta universe seems to have arrived
EF Core 数据过滤
Encryption and decryption of yii2
Redis:主从复制