当前位置:网站首页>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];
}
}
边栏推荐
- June training (day 12) - linked list
- SQL调优指南笔记18:Analyzing Statistics Using Optimizer Statistics Advisor
- 六月集训(第11天) —— 矩阵
- [qnx hypervisor 2.2 manuel de l'utilisateur] 4.2 environnement de construction pris en charge
- SQL调优指南笔记6:Explaining and Displaying Execution Plans
- Yyds dry goods inventory solution sword finger offer: the first non repeated character in the character stream
- Common error in script execution: build sh: caller: not found
- 六月集训(第12天) —— 链表
- Build a highly available database
- What is embedded
猜你喜欢

MySql主从复制

JUC并发工具包使用指南

JVisualVM初步使用

Shell script Basics

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

A puzzle about + =

ICML2022 | GALAXY:极化图主动学习

Oracle LiveLabs实验:Introduction to Oracle Spatial Studio
![[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

Okio source code analysis
随机推荐
图灵奖得主:想要在学术生涯中获得成功,需要注意哪些问题?
复杂系统如何检测异常?北卡UNCC等最新《复杂分布式系统中基于图的深度学习异常检测方法综述》,阐述最新图异常检测技术进展
SQL tuning guide notes 11:histograms
My struggle: my years in foreign enterprises (1)
ORM implements the mapping relationship between classes and tables, class attributes and fields
Recommended Chinese font in the code input box of Oracle SQL developer
Cookies and sessions
logstash时间戳转换为unix 纳秒nano second time
SQL调优指南笔记17:Importing and Exporting Optimizer Statistics
Yyds dry goods inventory solution sword finger offer: the first non repeated character in the character stream
Design and practice of Hudi bucket index in byte skipping
关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法
ICML2022 | GALAXY:极化图主动学习
Experiment 7-2-6 print Yanghui triangle (20 points)
Ubuntu 16.04 installing mysql5.6
A high-value MySQL management tool
经济学人聚焦WTO MC12:数字经济或成重要议题
[qnx hypervisor 2.2 manuel de l'utilisateur] 4.2 environnement de construction pris en charge
Turing prize winner: what should I pay attention to if I want to succeed in my academic career?
June training (day 10) - bit operation