当前位置:网站首页>How to abstract a problem into a 0-1 knapsack problem in dynamic programming
How to abstract a problem into a 0-1 knapsack problem in dynamic programming
2022-06-12 21:50:00 【InfoQ】
️ How to abstract the problem into 0-1 knapsack ️
The weight of the last stone II
stonesstones[i]ixyx <= y- If
x == y, Then both stones will be completely crushed ;
- If
x != y, So the weight isxThe stone will be completely crushed , And the weight isyThe new weight of the stone isy-x.
0 Input :stones = [2,7,4,1,8,1]
Output :1
explain :
Combine 2 and 4, obtain 2, So the array turns into [2,7,1,8,1],
Combine 7 and 8, obtain 1, So the array turns into [2,1,1,1],
Combine 2 and 1, obtain 1, So the array turns into [1,1,1],
Combine 1 and 1, obtain 0, So the array turns into [1], This is the optimal value .
Input :stones = [31,26,33,21,40]
Output :5
1 <= stones.length <= 30
1 <= stones[i] <= 100
Their thinking
+-+-aba>=ba-bcc-(a-b)a-b+d(a-b)-dabab+-+-+-+/-00+-+big_sum-small_sumbig_sum-small_sumsumsum=big_sum+small_sumbig_sum>=small_sumsmall_sum<=sum/2sum/2sumclass Solution {
public int lastStoneWeightII(int[] stones) {
int sum = 0;
for (int x : stones) {
sum += x;
}
int ret = sum / 2;
int n = stones.length;
int[][] dp = new int[n+1][ret+1];
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= ret; j++) {
int x = dp[i-1][j];
int y = j >= stones[i-1] ? dp[i-1][j-stones[i-1]] + stones[i-1] : 0;
dp[i][j] = Math.max(x, y);
}
}
return sum - 2 * dp[n][ret];
}
}
class Solution {
public int lastStoneWeightII(int[] stones) {
int sum = 0;
for (int x : stones) {
sum += x;
}
int ret = sum / 2;
int n = stones.length;
int[][] dp = new int[2][ret+1];
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= ret; j++) {
int index = (i-1) & 1;
int x = dp[index][j];
int y = j >= stones[i-1] ? dp[index][j-stones[i-1]] + stones[i-1] : 0;
dp[i&1][j] = Math.max(x, y);
}
}
return sum - 2 * dp[n&1][ret];
}
}
class Solution {
public int lastStoneWeightII(int[] stones) {
int sum = 0;
for (int x : stones) {
sum += x;
}
int ret = sum / 2;
int n = stones.length;
int[] dp = new int[ret+1];
for (int i = 1; i <= n; i++) {
for (int j = ret; j >= stones[i-1]; j--) {
int x = stones[i-1];
dp[j] = Math.max(dp[j], dp[j-x]+x);
}
}
return sum - 2 * dp[ret];
}
}
practice : Objectives and
numstarget'+''-'- for example ,
nums = [2, 1], Can be in2Before adding'+', stay1Before adding'-', And then concatenate them to get the expression"+2-1".
target Input :nums = [1,1,1,1,1], target = 3
Output :5
explain : Altogether 5 Ways to make the ultimate goal and 3 .
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3
Input :nums = [1], target = 1
Output :1
1 <= nums.length <= 20
0 <= nums[i] <= 1000
0 <= sum(nums[i]) <= 1000
-1000 <= target <= 1000
Problem solving
nums+/-target0target1numss--s+s0j0+/-itclass Solution {
public int findTargetSumWays(int[] nums, int target) {
// Dynamic programming
// State transition equation dp[i][j] = dp[i-1][j-nums[i]] + dp[i-1][j+nums[i]]
//i Represents the front of the array i Elements ,j Indicates goals and ,dp[i][j] It means before i In elements +/- obtain j Number of alternatives
// Consider the extreme , The sum of the absolute values of the target and the elements whose maximum value is the array is recorded as s, The minimum value is -s, therefore j The possible values of 2s+1 individual
//1. Find the sum of the absolute values of array elements
int s = 0;
for (int x : nums) {
s += Math.abs(x);
}
// If target The absolute value is greater than the maximum value of the array target sum , There can be no legal scheme
if (Math.abs(target) > s) {
return 0;
}
//2. Create dynamic programming array
int len = nums.length;
int[][] dp = new int[len + 1][2 *s + 1];
//3. initialization ,0->-s,s->0
dp[0][s] = 1;
//4. Deal with the rest
for (int i = 1; i <= len; i++) {
int t = nums[i - 1];
for (int j = -s; j <= s; j++) {
// In power, it is important to + when , If the legal scope is met , add dp[i - 1][j + s - t]
if (j + s - t >= 0) {
dp[i][j + s] += dp[i - 1][j + s - t];
}
// In power, it is important to - when , If the legal scope is met , add dp[i - 1][j + s + t]
if (j + s + t <= 2 * s) {
dp[i][j + s] += dp[i - 1][j + s + t];
}
}
}
return dp[len][target+s];
}
}
class Solution {
public int findTargetSumWays(int[] nums, int target) {
// Dynamic programming
// State transition equation dp[i][j] = dp[i-1][j-nums[i]] + dp[i-1][j+nums[i]]
//i Represents the front of the array i Elements ,j Indicates goals and ,dp[i][j] It means before i In elements +/- obtain j Number of alternatives
// Consider the extreme , The sum of the absolute values of the target and the elements whose maximum value is the array is recorded as s, The minimum value is -s, therefore j The possible values of 2s+1 individual
//1. Find the sum of the absolute values of array elements
int s = 0;
for (int x : nums) {
s += Math.abs(x);
}
// If target The absolute value is greater than the maximum value of the array target sum , There can be no legal scheme
if (Math.abs(target) > s) {
return 0;
}
//2. Create dynamic programming array
int len = nums.length;
int[][] dp = new int[2][2 *s + 1];
//3. initialization ,0->-s,s->0
dp[0][s] = 1;
//4. Deal with the rest
for (int i = 1; i <= len; i++) {
int t = nums[i - 1];
for (int j = -s; j <= s; j++) {
// When updating values , You need to zero the value of the original line
dp[i&1][j + s] = 0;
// If the value of the left boundary is satisfied , add dp[i - 1][j + s - t]
if (j + s - t >= 0) {
dp[i&1][j + s] += dp[(i-1)&1][j + s - t];
}
// If the value of the right boundary is satisfied , add dp[i - 1][j + s + t]
if (j + s + t <= 2 * s) {
dp[i&1][j + s] += dp[(i-1)&1][j + s + t];
}
}
}
return dp[len&1][target+s];
}
}
Optimization plan
class Solution {
public int findTargetSumWays(int[] nums, int target) {
// Dynamic programming
// For the first i Elements , The value is t, If you don't choose , Then the number of schemes dp[i-1][j], If you choose , Then the number of schemes dp[i-1][j-t], Total number of schemes dp[i][j]=dp[i-1][j]+dp[i-1][j-t].
//1. Find the sum of the absolute values of array elements
int s = 0;
for (int x : nums) {
s += Math.abs(x);
}
int m = (s - target) / 2;
// If target The absolute value is greater than the maximum value of the array target sum , perhaps s-target Not even numbers , There can be no legal scheme
if (Math.abs(target) > s || (s - target) % 2 != 0) {
return 0;
}
//2. Create dynamic programming array , One dimensional optimization
int len = nums.length;
int[][] dp = new int[len+1][m+1];
//3. initialization dp[0][0] = 1;
dp[0][0] = 1;
//4. Deal with the rest
for (int i = 1; i <= len; i++) {
int t = nums[i - 1];
for (int j = 0; j <= m; j++) {
dp[i][j] = dp[i-1][j];
if (j >= t) {
dp[i][j] += dp[i-1][j-t];
}
}
}
return dp[len][m];
}
}
class Solution {
public int findTargetSumWays(int[] nums, int target) {
// Dynamic programming
// For the first i Elements , The value is t, If you don't choose , Then the number of schemes dp[i-1][j], If you choose , Then the number of schemes dp[i-1][j-t], Total number of schemes dp[i][j]=dp[i-1][j]+dp[i-1][j-t].
//1. Find the sum of the absolute values of array elements
int s = 0;
for (int x : nums) {
s += Math.abs(x);
}
int m = (s - target) / 2;
// If target The absolute value is greater than the maximum value of the array target sum , perhaps s-target Not even numbers , There can be no legal scheme
if (Math.abs(target) > s || (s - target) % 2 != 0) {
return 0;
}
//2. Create dynamic programming array , One dimensional optimization
int len = nums.length;
int[][] dp = new int[2][m+1];
//3. initialization dp[0][0] = 1;
dp[0][0] = 1;
//4. Deal with the rest
for (int i = 1; i <= len; i++) {
int t = nums[i - 1];
for (int j = 0; j <= m; j++) {
dp[i&1][j] = dp[(i-1)&1][j];
if (j >= t) {
dp[i&1][j] += dp[(i-1)&1][j-t];
}
}
}
return dp[len&1][m];
}
}
class Solution {
public int findTargetSumWays(int[] nums, int target) {
// Dynamic programming
// For the first i Elements , The value is t, If you don't choose , Then the number of schemes dp[i-1][j], If you choose , Then the number of schemes dp[i-1][j-t], Total number of schemes dp[i][j]=dp[i-1][j]+dp[i-1][j-t].
//1. Find the sum of the absolute values of array elements
int s = 0;
for (int x : nums) {
s += Math.abs(x);
}
int m = (s - target) / 2;
// If target The absolute value is greater than the maximum value of the array target sum , perhaps s-target Not even numbers , There can be no legal scheme
if (Math.abs(target) > s || (s - target) % 2 != 0) {
return 0;
}
//2. Create dynamic programming array , One dimensional optimization
int len = nums.length;
int[] dp = new int[m+1];
//3. initialization dp[0] = 1;
dp[0] = 1;
//4. Deal with the rest
for (int i = 1; i <= len; i++) {
int t = nums[i - 1];
for (int j = m; j >= t; j--) {
dp[j] += dp[j-t];
}
}
return dp[m];
}
}
边栏推荐
- [target detection] |dive detector into box for object detection new training method based on fcos
- 回文链表及链表相交问题(和心怡的人相交)你真的会了吗?
- Thread safe level
- SQL tuning guide notes 14:managing extended statistics
- CVPR 2022 | 应对噪声标签,西安大略大学、字节跳动等提出对比正则化方法
- [qnx hypervisor 2.2 manuel de l'utilisateur] 4.2 environnement de construction pris en charge
- 同花顺能开户吗,在APP上可以直接开通券商安全吗 ,证券开户怎么开户流程
- gzip压缩解压缩
- Ansible-大总结(六)
- Redis cluster mget optimization
猜你喜欢

SQL调优指南笔记6:Explaining and Displaying Execution Plans

建立高可用的数据库

Graphics2D类基本使用

SQL调优指南笔记17:Importing and Exporting Optimizer Statistics

User guide for JUC concurrency Toolkit

图灵奖得主:想要在学术生涯中获得成功,需要注意哪些问题?
![[target detection] |dive detector into box for object detection new training method based on fcos](/img/ac/c54c2733dceffea086b772f35f128a.png)
[target detection] |dive detector into box for object detection new training method based on fcos

Smart management of green agriculture: a visual platform for agricultural product scheduling

最近公共祖先问题你真的学会了吗?

Ansible-大总结(六)
随机推荐
Oracle 19C installation documentation
SQL tuning guide notes 16:managing historical optimizer statistics
Ubuntu16.04 completely delete MySQL database
SQL调优指南笔记13:Gathering Optimizer Statistics
Design and practice of Hudi bucket index in byte skipping
logstash时间戳转换为unix 纳秒nano second time
ORM implements the mapping relationship between classes and tables, class attributes and fields
关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法
[target detection] |dive detector into box for object detection new training method based on fcos
DRF receives nested data and creates objects. Solution: DRF not NULL constraint failed
drf 接收嵌套数据并创建对象, 解决:drf NOT NULL constraint failed
SQL调优指南笔记9:Joins
SQL调优指南笔记17:Importing and Exporting Optimizer Statistics
Zip compression decompression
Linux backup MySQL
同花顺能开户吗,在同花顺开户安全么,证券开户怎么开户流程
SQL调优指南笔记16:Managing Historical Optimizer Statistics
linux备份mysql
Cloning PDB with ADG standby
六月集训(第12天) —— 链表