1.力扣122--买卖股票的最佳时机2
class Solution { public int maxProfit(int[] prices) { int n = prices.length; int[][] dp = new int[n][2]; //二维动态规划, dp[0][0] = 0;dp[0][1] = -prices[0]; //dp[i][0]代表当前不持有股票的最大收益,dp[i][1]代表当前持有股票的最大收益 for(int i = 1;i<n;i++){ dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]+prices[i]); dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0]-prices[i]); } return dp[n-1][0]; } }
2.力扣309--最佳买卖股票时机含冷冻期
class Solution { public int maxProfit(int[] prices) { int n = prices.length; int[][] dp = new int[n][3]; //三维动态规划 dp[0][0] = 0; dp[0][1] = 0; dp[0][2] = -prices[0]; for(int i = 1;i<n;i++){ //dp[i][0]代表卖出的最大值, dp[i][0] = Math.max(Math.max(dp[i-1][0],dp[i-1][1]),dp[i-1][2]+prices[i]); //dp[i][1]代表冷冻期的值,该值只能是上一轮卖出的值 dp[i][1] = dp[i-1][0]; //dp[i][2]代表买入后的最大值,注意实际买入只能在冷冻期后面就是dp[i][1]后面 dp[i][2] = Math.max(dp[i-1][1]-prices[i],dp[i-1][2]); } return Math.max(dp[n-1][0],dp[n-1][1]); } }
3.acwing2--01背包问题
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), capcity = in.nextInt(); int[][] nums = new int[n][2]; for(int i = 0;i<n;i++){ int a = in.nextInt(), b = in.nextInt(); nums[i][0] = a; nums[i][1] = b; } System.out.println(packageProblem(nums,capcity)); } public static int packageProblem(int[][] nums, int capcity){ int[][] dp = new int[nums.length+1][capcity+1]; for(int i = 1;i<=nums.length;i++){ for(int j = 1;j<=capcity;j++){ dp[i][j] = dp[i-1][j]; if(nums[i-1][0]<=j){ dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i - 1][0]] + nums[i - 1][1]); } } } int res = 0; for(int i = 1;i<=capcity;i++){ res = Math.max(res,dp[nums.length][i]); } return res; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), capcity = in.nextInt(); int[][] nums = new int[n][2]; for(int i = 0;i<n;i++){ int a = in.nextInt(), b = in.nextInt(); nums[i][0] = a; nums[i][1] = b; } System.out.println(packageProblem(nums,capcity)); } public static int packageProblem(int[][] nums, int capcity){ int[] dp = new int[capcity+1]; for(int i = 1;i<=nums.length;i++){ for(int j = capcity;j>=nums[i-1][0];j--){ dp[j] = Math.max(dp[j-nums[i-1][0]] + nums[i-1][1],dp[j]); } } return dp[capcity]; } }
4.acwing3--完全背包问题
import java.util.Scanner; public class acwing3 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), capcity = in.nextInt(); int[][] nums = new int[n][2]; for(int i = 0;i<n;i++){ nums[i][0] = in.nextInt(); nums[i][1] = in.nextInt(); } int[] dp = new int[capcity+1]; for(int i = 0;i<n;i++){ for(int j = nums[i][0];j<=capcity;j++){ dp[j] = Math.max(dp[j],dp[j-nums[i][0]] + nums[i][1]); } } System.out.println(dp[capcity]); } }
5.力扣322--零钱兑换
class Solution { public int coinChange(int[] coins, int amount) { //完全背包问题的动态规划 int n = coins.length; int[] dp = new int[amount+1]; for(int i = 0;i<=amount;i++){ dp[i] = 0x3f3f3f3f; } dp[0] = 0; for(int i = 0;i<n;i++){ //不同轮代表用到的不同硬币 for(int j = coins[i];j<=amount;j++){ //dp[j]代表这一轮中容量为j的时候的最小硬币个数 //因为这里的硬币是可以重复使用的,所以应该从前向后遍历 dp[j] = Math.min(dp[j],dp[j-coins[i]]+1); } System.out.println(dp[amount]); } if(dp[amount] == 0x3f3f3f3f){ return -1; } return dp[amount]; } }
标签:25,Scanner,--,int,2023.1,new,public,dp From: https://www.cnblogs.com/lyjps/p/17067018.html