当前位置:网站首页>动态规划、背包问题 6/23 101-105
动态规划、背包问题 6/23 101-105
2022-08-10 05:36:00 【吉良吉影__.】
1.买卖股票的最佳时机( LeetCode 121 )
动态规划
class Solution {
public int maxProfit(int[] prices) {
if(prices.length <= 1)return 0;
int max = 0;//最大利润
int min = prices[0];//min维护当天之前的最下的一天的股票价格
for (int i = 1; i < prices.length; i++) {
max = Math.max(max,prices[i] - min);
if (prices[i] < min)min = prices[i];
}
return max;
}
}
2.买卖股票的最佳时机II( LeetCode 122 )
方法一:贪心算法
class Solution {
public int maxProfit(int[] prices) {
if (prices.length <= 1)return 0;
boolean flag = false;//是否持有股票
int ans = 0;
int pre = 0;//手中刚买的股票
for (int i = 0; i < prices.length; i++) {
if (!flag){
//当前手中未持股
flag = true;
pre = prices[i];//买入股票
}else if (prices[i] < pre){
//今天更便宜今天买入
pre = prices[i];
}else if (i + 1 < prices.length && prices[i + 1] < prices[i]){
//明天卖出会更少赚一点
ans += prices[i] - pre;
flag = false;
}else if (i == prices.length - 1){
ans += prices[i] - pre;
flag = false;
}
}
return ans;
}
}
方法二:官方题解,贪心算法
class Solution {
public int maxProfit(int[] prices) {
int ans = 0;
int n = prices.length;
for (int i = 1; i < n; ++i) {
ans += Math.max(0, prices[i] - prices[i - 1]);
}
return ans;
}
}
方法三:动态规划
维护两个状态
class Solution {
public int maxProfit(int[] prices) {
if (prices.length <= 1) return 0;
int[][] dp = new int[prices.length][2];
dp[0][0] = 0;//第0天结束未持有股票的最大收益
dp[0][1] = -prices[0];//第0天结束持有股票的最大收益
for (int i = 1; i < prices.length; 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[prices.length - 1][0];
}
}
方法四:动态规划 优化
class Solution {
public int maxProfit(int[] prices) {
if (prices.length <= 1) return 0;
int x = 0;//当天未持有股票最大收益
int y = - prices[0];//当天持有股票最大收益
for (int i = 1; i < prices.length; i++) {
x = Math.max(x, y + prices[i]);
y = Math.max(y, x - prices[i]);
}
//最后一天未持有股票肯定更大
return x;
}
}
3.买卖股票的最佳时机III( LeetCode 123 )hard
动态规划
维护四个状态
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int buy1 = -prices[0], sell1 = 0;
int buy2 = -prices[0], sell2 = 0;
for (int i = 1; i < n; ++i) {
buy1 = Math.max(buy1, -prices[i]);
sell1 = Math.max(sell1, buy1 + prices[i]);
buy2 = Math.max(buy2, sell1 - prices[i]);
sell2 = Math.max(sell2, buy2 + prices[i]);
}
return sell2;
}
}
4.买卖股票的最佳时机IV( LeetCode 188 )hard
动态规划
参考第三题
class Solution {
public int maxProfit(int k, int[] prices) {
int n = prices.length;
if (n < 1 || k < 1)return 0;//临界条件prices为空,或者k = 0一次股票都不能买时
int[][] buyOrSell = new int[k][2];
//buyOrSell[i][j] j = 0 时表示第i天结束手中持有股票的最大利润
//buyOrSell[i][j] j = 1 时表示第i天结束手中没有持有股票的最大利润
for (int i = 0; i < k; i++) {
buyOrSell[i][0] = -prices[0];
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < k; j++) {
//当前为第j次购买股票
//第i天结束时手中若有股票
//第i结束时有股票的最大利润 = Math.max(前一天手中有第j次买入股票的最大利润,
// 前一天售完第j - 1次股票的最大利润 + prices[i])
int x = j == 0 ? 0 : buyOrSell[j - 1][1];
buyOrSell[j][0] = Math.max(buyOrSell[j][0],x - prices[i]);
buyOrSell[j][1] = Math.max(buyOrSell[j][1],buyOrSell[j][0] + prices[i]);
}
}
return buyOrSell[k - 1][1];
}
}
5.最佳买卖股票时机含冷冻期(LeetCode 309)
动态规划
参考上面的几道题
class Solution {
public int maxProfit(int[] prices) {
if (prices.length < 1)return 0;
int buy = -prices[0];//当天结束手中有入股票
int sellno = 0;//当天结束手中没有股票,且当天没有出售股票
int sell = 0;//当天结束手中没有股票,且当天有出售股票
for (int i = 1; i < prices.length; i++) {
buy = Math.max(buy,sellno - prices[i]);
sellno = Math.max(sellno,sell);
sell = buy + prices[i];
}
return Math.max(sellno,sell);
}
}
边栏推荐
- STM32单片机LORA无线远程火灾报警监控系统DS18B20MQ2火焰检测
- LeetCode refers to the offer 21. Adjust the order of the array so that the odd numbers are in front of the even numbers (simple)
- 以STM32F103C6TA为例通过配置CubeMX实现GPIO输出完成点灯实例
- LaTeX总结----在CSDN上写出数学公式
- 【C语言】结构体变量学习笔记1
- (Flutter报错)Cannot run with sound null safety, because the following dependencies
- 通过adb devices命令在控制台显示企业级PicoNeo3设备号
- 工业废酸回收工艺
- 在Unity中判断游戏物体是否在游戏屏幕范围之内
- C陷阱与缺陷 个人阅读笔记
猜你喜欢
随机推荐
溶液中重金属去除
C陷阱与缺陷 个人阅读笔记
为什么游戏需要热更新?
【接口自动化】
电路建模的要点
开源游戏服务器框架NoahGameFrame(NF)客户端环境搭建(三)
接口自动化2.0
【简易笔记】PyTorch官方教程简易笔记 EP1
GC0053-STM32单片机NTC热敏电阻温度采集及控制LCD1602
Unity对象池实现
请亲们关注下我,谢谢了。
多线程与多进程(概念详细讲解)
STM32单片机OLED俄罗斯方块单片机小游戏
STM32F407ZG TIM通用定时器
STM32F407ZG GPIO输入相关实验
剑指 Offer(第 2 版)7/7 14-17
以STM32F103C6T6为例通过配置CubeMX实现EXIT外部中断
电池级碳酸氢锂除杂质钙镁离子工艺原理
LaTeX总结----在CSDN上写出数学公式
STM32单片机RGB红蓝调光植物补光系统红光蓝光PWM调色调节亮度