当前位置:网站首页>Leetcode 416. Split equal sum subset
Leetcode 416. Split equal sum subset
2022-06-12 18:41:00 【Changersh】
416. To divide into equal and subsets
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
Ideas
Put the whole collection , Divided into two subsets , Ask if two subsets can and equal
therefore , The capacity of a backpack is the sum of the set / 2, Of course , The capacity must be an integer , So when sum It must not be satisfied when it is an odd number , return false
- dp[i]: Represents when the capacity is i The largest sum of time
- because dp[i] There should be two situations ,1 Is to add that the current number does not meet the capacity i So no more numbers ,2 Yes, plus the current number, it still meets the capacity i, therefore dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]);
- initialization : Because when the capacity is 0 The maximum capacity is also 0, Array has no negative value , Don't worry about initial value overrides dp value , So all are initialized to 0 that will do
- traversal order : from 2 The recursion formula of can get the first traversal of the article , Post traversal capacity , And because it's 0-1 knapsack , One dimensional scrolling array , So the capacity is traversed from back to front
Code
class Solution {
public:
// 1. All sums can be divided into two sets , Description and yes even numbers
// 2. So the backpack capacity is sum / 2
bool canPartition(vector<int>& nums) {
int sum = 0;
for (int i = 0; i < nums.size(); i++)
sum += nums[i];
if (sum & 1) return false; // Odd number , immediate withdrawal
sum /= 2;
int dp[sum + 1];
for (int i = 0; i < sum + 1; i++)
dp[i] = 0;
for (int i = 0; i < nums.size(); i++) {
for (int j = sum; j >= nums[i]; j--)
dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]);
}
if (dp[sum] == sum) return true;
return false;
}
};
边栏推荐
- Pytest automated testing framework (II)
- Gd32f4xx communicates with electric energy meter conforming to dlt645_ two
- Problems that the sap Spartacus e-commerce cloud UI shipping method does not display in the unit test environment
- VirtualLab basic experiment tutorial -4 Single slit diffraction
- Hugo 博客搭建教程
- 间隔两个月,我的第二次上榜纪念日【2022.6.2】
- Review of MySQL (3): query operation
- Title 54: take 4 ~ 7 bits of an integer a from the right end.
- CEPH deploy offline deployment of CEPH cluster and error reporting FAQ
- leetcode:5259. 计算应缴税款总额【简单模拟 + 看看在哪个区间】
猜你喜欢

How to modify the authorization of sina Weibo for other applications

基于halcon—缺陷检测常用方法与示例总结

Machine learning series (3): logistic regression

Why my order by create_ Time ASC becomes order by ASC

Typescript common types (I)

Double non grind one, three side byte, cool. Next time

Mise en œuvre de l'ACL réflexe dans le simulateur Cisco Cisco Packet Tracer

Title 68: there are n integers, so that the previous numbers are moved backward m positions, and the last m numbers become the first m numbers

GD32F4xx控制DGUS 变量显示

kali通过iptables实现端口转发功能
随机推荐
Installation and configuration of window version pytorch entry depth learning environment
VirtualLab基礎實驗教程-4.單縫衍射
Two months later, my second listing anniversary [June 2, 2022]
C语言学习——数据在内存中的存储
Machine learning series (5): Naive Bayes
即时配送的订单分配策略:从建模和优化-笔记
Redis(三十二)-用Redis做分布式锁
在思科模擬器Cisco Packet Tracer實現自反ACL
kali2022如何安装w3af
【矩阵论 & 图论】期末考试复习思维导图
What is SAP support package stack
Implementing reflexive ACL in Cisco packet tracker
从应无所住说起
实验10 Bezier曲线生成-实验提高-控制点生成B样条曲线
Quickly copy the request in browser F12 to postman/ or generate the corresponding code of the relevant language
用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性
Partial scratch and corrosion detection of bolts and screws based on Halcon
【sql语句基础】——查(select)(单表查询)
Pytest automated testing framework (II)
torch.where的新用法(很老但是大家忽略的用法)