当前位置:网站首页>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 ]
边栏推荐
猜你喜欢

什么是自签名证书?自签名SSL证书的优缺点?

SSL证书七大常见错误及解决方法

Simple distinction between break and continue

Merge sort

What files does a CA digital certificate contain? How to view SSL certificate information?

归并排序

SSL证书格式转化的两种方法
![[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect](/img/96/e3afb2b9683cce7645afd483cc0844.png)
[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect

33Mysql

速看 2021-2022年23项重大网络犯罪统计数据
随机推荐
Heap sort
dhu编程练习
How long is the general term of the bank's financial products?
How to use SMS to deliver service information to customers? The guide is here!
Quick sort
Jenkins continuous integration environment build 8 (configure mailbox server to send build results)
DHU programming exercise
FDA mail security solution
SQL injection -day17
CA数字证书包含哪些文件?如何查看SSL证书信息?
选购通配符SSL证书注意事项
dhu编程练习
打造创客教育中精湛技艺
Radware warns about the next round of DDoS Attacks
有流量,但没有销售?增加网站销量的 6 个步骤
速看 2021-2022年23项重大网络犯罪统计数据
Creating exquisite skills in maker Education
银行的理财产品一般期限是多久?
FDA ESG regulation: digital certificate must be used to ensure communication security
Five cheapest wildcard SSL certificate brands