当前位置:网站首页>2. < tag dynamic programming and 0-1 knapsack problem > lt.416 Split equal sum subset + lt.1049 Weight of the last stone II
2. < tag dynamic programming and 0-1 knapsack problem > lt.416 Split equal sum subset + lt.1049 Weight of the last stone II
2022-06-30 02:27:00 【Caicai's big data development path】
lt.416. To divide into equal and subsets
[ Case needs ]

[ Train of thought analysis 1 , Dynamic programming ]


[ Code implementation ]
class Solution {
public boolean canPartition(int[] nums) {
// The volume of the backpack is Sum/2;
// The goods to be put in the backpack ( The elements in the collection ) Weight is The value of the element , The value is also the value of the element ;
// The backpack is just full , Indicates that the sum of Sum / 2 Subset ;
// Every element in the backpack cannot be put in repeatedly
//1. determine DP The meaning of arrays and subscripts
//0-1 knapsack problem , dp[i][j], 0~i-1 The numbered items are put into a container with a capacity of j The maximum value you get from your backpack
// This topic : dp[j] Indicates that the backpack capacity is j, The maximum can be made up j The sum of the subsets of is dp[j]
int sum = 0;
int n = nums.length;
int target = 0;
for(int x : nums){
sum += x;}
if(sum % 2 != 0)return false;
target = sum / 2;
int[] dp = new int[sum / 2 + 1];
//2. Determine the recurrence formula
//0,1 knapsack : dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
//dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]);
for(int i = 0; i < n; i++){
for(int j = target; j >= nums[i]; j--){
dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]);
}
}
return dp[target] == target;
}
}
lt.1049. The weight of the last stone II
[ Case needs ]

[ Thought analysis ]
[ Code implementation ]
边栏推荐
- 2.< tag-动态规划和0-1背包问题>lt.416. 分割等和子集 + lt.1049. 最后一块石头的重量 II
- 桶排序
- Weekly recommended short video: why is the theory correct but can not get the expected results?
- IBM websphere通道联通搭建和测试
- The odoo15 page jumps directly to the editing status
- Large scale DDoS attacks and simulated DDoS tests against VoIP providers
- DigiCert Smart Seal是什么?
- True love forever valentine's Day gifts
- Shell Sort
- CTF introductory learning (WEB direction)
猜你喜欢

Quick sort

26. common interview questions of algorithm

IBM websphere通道联通搭建和测试

PR second training notes
![[on] [DSTG] dynamic spatiotemporalgraph revolutionary neural networks for traffic data impact](/img/c3/f9d6399c931a006ca295bb1e3ac427.png)
[on] [DSTG] dynamic spatiotemporalgraph revolutionary neural networks for traffic data impact

Shenzhen CPDA Data Analyst Certification in July 2022

FDA ESG规定:必须使用数字证书保证通信安全

What is digicert smart seal?

什么是证书透明度CT?如何查询CT logs证书日志?

DigiCert Smart Seal是什么?
随机推荐
直接插入排序
Weekly recommended short video: why is the theory correct but can not get the expected results?
How to create a CSR (certificate signing request) file?
选购通配符SSL证书注意事项
FDA邮件安全解决方案
matlab代码运行教程(如何运行下载的代码)
The largest DDoS attack ever peaked at 400 Gbps
Differences among digicert, SECTIONO and globalsign code signing certificates
SSL证书七大常见错误及解决方法
dhu编程练习
五个最便宜的通配符SSL证书品牌
DMX configuration
网上炒股安全么?炒股需要开户吗?
NCA: the nine year old has launched a DDoS attack
dhu编程练习
Day_ 19 multithreading Basics
Blue Bridge Cup stm32g431 - three lines of code for keys (long press, short press, click, double click)
How do PMP candidates respond to the new exam outline? Look!
[on] [DSTG] dynamic spatiotemporalgraph revolutionary neural networks for traffic data impact
【postgres】postgres 数据库迁移