应用场景-背包问题
动态规划算法介绍
- 动态规划(Dynamic Programming)算法的核心思想是:将大问题划分为小问题进行解决,从而一步步获取最优解的处理算法
- 动态规划算法与分治算法类似,其基本思想也是将待求解问题分解成若干个子问题,先求解子问题,然后从这些子问题的解得到原问题的解。
- 与分治法不同的是,适合于用动态规划求解的问题,经分解得到子问题往往不是互相独立的。(即下一个子阶段的求解是建立在上一个子阶段的解的基础上,进行进一步的求解)
- 动态规划可以通过填表的方式来逐步推进,得到最优解
背包问题思路分析
import org.junit.Test;
/**
* @author wty
* @date 2022/11/2 9:46
* 动态规划实现背包问题
*/
public class EX01 {
@Test
public void EX01Test(){
// 物品重量
int []w = {1,4,3};
// 物品的价值
int []value = {1500,3000,2000};
// 背包容量
int C = 4;
// 物品的个数
int count = w.length;
// 创建二维数组
// v[i][j] 表示在前i个物品中能够装入容量为j的背包中价值的最大值
int [][]v = new int[count+1][C+1];
// 定义一个数组记录商品存放情况
int path[][] = new int[count+1][C+1];
// 初始化第一行和第一列,数组可以省略
// 初始化列
for (int i = 0; i < v.length; i++) {
v[i][0] = 0;
}
// 初始化行
for (int j = 0; j < v[0].length; j++) {
v[0][j] = 0;
}
System.out.println("====动态规划前====");
for (int i = 0; i < v.length; i++) {
for (int j = 0; j < v[i].length; j++) {
System.out.print(v[i][j] + "\t");
}
System.out.println();
}
// 根据公式动态规划处理
for (int i = 1; i < v.length; i++) {
for (int j = 1; j < v[i].length; j++) {
// 程序从1开始
if (j < w[i-1]){
v[i][j] = v[i-1][j];
}else {
// 这里value[i-1]要注意从1开始的 [j-w[i-1]]同理
//v[i][j] = Math.max(v[i][j],value[i-1] + v[i-1][j-w[i-1]]);
// 为了记录商品存放背包的关系要用if和else实现
if (v[i][j] < value[i-1] + v[i-1][j-w[i-1]]){
v [i][j] = value[i-1] + v[i-1][j-w[i-1]];
// 记录当前的情况
path[i][j] = 1;
}else{
v [i][j] = v[i][j];
}
}
}
}
System.out.println("====动态规划后====");
for (int i = 0; i < v.length; i++) {
for (int j = 0; j < v[i].length; j++) {
System.out.print(v[i][j] + "\t");
}
System.out.println();
}
System.out.println("====path数组====");
for (int i = 0; i < path.length; i++) {
for (int j = 0; j < path[i].length; j++) {
System.out.print(path[i][j] + "\t");
}
System.out.println();
}
/**
* path数组,如果放入新物品就置为1
* 0 0 0 0 0
* 0 1 1 1 1
* 0 0 0 0 1
* 0 0 0 1 1
*/
// 行数
int i = path.length -1;
// 列数
int j = path[0].length -1;
System.out.println(i);
System.out.println(j);
while (i>0 && j >0){
if (path[i][j] == 1){
System.out.println("第"+i+"次,放入背包");
j = j-w[i-1];
}
--i;
}
}
}
LeetCode 买卖股票的最佳时机 II
买卖股票的最佳时机 II
给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
返回 你能获得的 最大 利润 。
示例 1:
输入:prices = [7,1,5,3,6,4]
输出:7
解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。
随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3 。
总利润为 4 + 3 = 7 。
示例 2:
输入:prices = [1,2,3,4,5]
输出:4
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。
总利润为 4 。
示例 3:
输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0 。
提示:
1 <= prices.length <= 3 * 104
0 <= prices[i] <= 104
代码示例:
方法一:算取相邻2天差值,如果有利润就相加求和
public class Day02 {
@Test
public void Day02Test(){
int array[] = {7,1,5,3,6,4};
//int array[] = {1,2,3,4,5};
//int array[] = {7,6,4,3,1};
//int num = new Solution02().maxProfit(array);
int num = new Solution02().maxProfit02(array);
System.out.println(num);
}
}
class Solution02 {
public int maxProfit(int[] prices) {
int lr = 0;
int size = prices.length;
for (int right = 1; right < size; right++) {
lr = lr + Math.max(0,prices[right] - prices[right - 1]);
}
return lr;
}
}
方法二:动态规划
public class Day02 {
@Test
public void Day02Test(){
int array[] = {7,1,5,3,6,4};
//int array[] = {1,2,3,4,5};
//int array[] = {7,6,4,3,1};
//int num = new Solution02().maxProfit(array);
int num = new Solution02().maxProfit02(array);
System.out.println(num);
}
}
class Solution02 {
public int maxProfit02(int[] prices) {
// 总天数
int size = prices.length;
if (size < 2){
return 0;
}
// 初始化条件
int dp[][] = new int[size][2];
// 开始第一天就有股票
dp[0][1] = -prices[0];
// 开始第一天手上没有股票
dp[0][0] = 0;
for (int i = 1; i < size; i++) {
// 第i天没有股票
dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
// 第i天有股票
dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]-prices[i]);
}
return dp[size-1][0];
}
}