当前位置:网站首页>LeetCode_ 416_ Divide equal sum subsets
LeetCode_ 416_ Divide equal sum subsets
2022-07-29 10:49:00 【Fitz1318】
Topic link
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 <= 2001 <= nums[i] <= 100
Their thinking
Dynamic programming
- The volume of the backpack is sum/2
- Goods to be put in the backpack ( The elements in the set ) The weight is the value of the element , Value is also the value of the element
- If the backpack is just full , Indicates that the sum of sum/2 Subset
- Every element in the backpack cannot be put in repeatedly
Dynamic programming five steps
- determine
dpThe meaning of array and subscriptdp[j]: Capacity ofjThe backpack , The maximum can be made upjThe sum of the subsets of isdp[j]
- Determine the recurrence formula
- stay 01 In the backpack , The recurrence formula is
dp[j] = Math.max(dp[j],dp[j-weight[i] + value[i]]),dp[j] = Math.max(dp[j],dp[j-nums[i]] + nums[i])
- stay 01 In the backpack , The recurrence formula is
dpArray initializationdp[0] = 0, Other subscriptsdp[i] = 0
- Determine the traversal order
- Traversal of items for Loop on the outer layer , Traversing the backpack for Loop on the inner layer , And inner for Loop backward traversal
- For example, push to
dpArray- If dp[j] == j, It means that the sum of subsets in the set can just be summed up
j
- If dp[j] == j, It means that the sum of subsets in the set can just be summed up
AC Code
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;
}
}
边栏推荐
- HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
- 98. (cesium chapter) cesium point heat
- Mongodb aggregation statistics
- Achieve the effect of a menu tab
- 重磅 | 2022 开放原子全球开源峰会在北京开幕
- StarRocks 技术内幕:实时更新与极速查询如何兼得
- 敏捷开发如何消减协作中的认知偏差?| 敏捷之道
- Add: create Ou structure using PowerShell
- NUMA architecture CPU API change summary
- What are the compensation standards for hospital misdiagnosis? How much can the hospital pay?
猜你喜欢

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

主子仓库都修改,如何进行同步?

一文搞懂什么是二叉树(二叉树的种类、遍历方式、定义)

This is the right way for developers to open artifacts

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

remap_ Use of table in impdp

Performance optimization analysis tool | perf

数据可视化设计指南(信息图表篇)

重磅 | 2022 开放原子全球开源峰会在北京开幕

Learning R language these ebooks are enough!
随机推荐
2018-UperNet ECCV
Follow teacher Wu to learn advanced numbers - function, limit and continuity (continuous update)
HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
Mitsubishi PLC and Siemens PLC
3道软件测试面试题,能全答对的人不到10%!你会几个?
Error: Protobuf syntax version should be first thing in file
Why use markdown to write?
Kunlun storage vs PostgreSQL OLTP test
2022cuda summer training camp Day5 practice
R包pedquant实现股票下载和金融量化分析
This is the right way for developers to open artifacts
98. (cesium chapter) cesium point heat
Mongodb aggregation statistics
What are the compensation standards for hospital misdiagnosis? How much can the hospital pay?
[untitled]
js两个数组对象进行合并去重
8.穿插-从架构设计到实践理解ThreadPoolExecutor线程池
QT工程基本构建
Achieve the effect of a menu tab
R language uses data set veteran for survival analysis