当前位置:网站首页>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;
}
}
边栏推荐
- Drunken driving alarm system based on stm32
- 【Unity,C#】Character键盘输入转向与旋转
- factoextra:多元统计的可视化
- PAHO cross compilation
- Survival analysis using rtcga clinical data
- A tour of grp:04 - GRP unary call unary call
- ES6-箭头函数this指向
- 周鸿祎:360是世界上最大的安全大数据公司
- How can agile development reduce cognitive bias in collaboration| Agile way
- 3道软件测试面试题,能全答对的人不到10%!你会几个?
猜你喜欢

StarRocks 技术内幕:实时更新与极速查询如何兼得

HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态

Drunken driving alarm system based on stm32

Kunlun storage vs PostgreSQL OLTP test

How can agile development reduce cognitive bias in collaboration| Agile way

Open source, compliance escort! 2022 open atom global open source summit open source compliance sub forum is about to open

1. (map tools) detailed tutorial of acrgis desktop10.5 software installation

Implementation of college logistics repair application system based on SSM

12th generation core processor +2.8k OLED ASUS good screen, lingyao 142022 shadow cyan glaze business thin book

浅谈安科瑞灭弧式智慧用电在养老机构的应用
随机推荐
从零开始Blazor Server(3)--添加cookie授权
会议OA项目----我的审批
Two MySQL tables with different codes (utf8, utf8mb4) are joined, resulting in index failure
Static resource mapping
Is error log monitoring enough? Don't try JVM monitoring of microservices
周鸿祎:360是世界上最大的安全大数据公司
Oncopy and onpaste
[dark horse morning post] Youxian responded to the dissolution every day, and many places have been unable to place orders; Li Bin said that Wei Lai will produce a mobile phone every year; Li Ka Shing
Data visualization design guide (information chart)
AI模型风险评估 第2部分:核心内容
LeetCode二叉树系列——144.二叉树的前序遍历
Factoextra: visualization of multivariate statistics
Meeting OA project (V) -- meeting notice and feedback details
深度强化学习应用实践技巧
VMWare:使用命令更新或升级 VMWare ESXi 主机
Luogu p1816 loyalty solution
R 语言 二分法与 牛顿迭代法计算中方程的根
架构实战营模块八作业
服务器
美团、饿了么被杭州市监约谈要求落实食品安全管理责任 严禁恶意竞争