当前位置:网站首页>LeetCode 121:买卖股票的最佳时机
LeetCode 121:买卖股票的最佳时机
2022-07-26 21:38:00 【斯沃福德】
链接
题目:
思路:动态规划
1. 状态
这个问题的「状态」有三个:
第⼀个是天数 i,
第⼆个是允许交易的最大次数 k,
第三个是当前的持有状态(即 之前说的 rest 的状态,我们不妨⽤ 1 表示持有,0 表示没有持有)。然后我们⽤⼀个三维数组就可以装下 这⼏种状态的全部组合:
比如说 dp[3][2][1] 的含义就是:今天是第三天,现在⼿上持有着股票,⾄今最多进⾏ 2 次交易;
dp[2][3][0] 的含义:今天是第⼆天,我现在⼿上没有持有股票,至今最多进⾏ 3 次交易;
题目所求:dp[n - 1][K][0],即最后⼀天,最多允许 K 次交易,最多获得多少利润
最后一个rest状态一定是0,即卖出了股票,利润才可能最大;
即求最大利润时,手里的股票一定没有股票!
2. 状态转移方程:
情况 1:今天没有持有股票
状态转移方程:
- 昨天没有持有 即rest=0,且截至昨天最⼤交易次数限制为 k;然后今天不买,依然没有股票,最⼤交易次数限制依然为 k。
- 昨天持有股票,即rest=1,截至昨天最大交易次数限制为 k;但是今天卖出去了,所以今天没有持有股票了,最⼤交易次数限制依然为 k。
情况2:今天持有股票
状态转移方程:
- 昨天持有股票,即rest=1,且截⾄昨天最⼤交易次数限制为 k;所以今天还持有着股票,最⼤交易次数限制依然为 k。
- 昨天没有持有,rest=0,且截⾄昨天最⼤交易次数限制为 k - 1;但今天选择 buy买入,所以今天我就持有股票了,最⼤交易次数限制为 k ??
.
3. base case
由于 i 从0开始,而状态方程中存在 [i-1] ,所以将dp[i-1]先赋值;
未开始时利润为0,不合理的值设为Integer.MIN_VALUE
而最大交易次数 K是从1开始的,k=0的不存在,利润为0 ;
且k=0时不允许持有股票,不合理的值设为Integer.MIN_VALUE;
由于k限制为1,所以此题去掉第二个状态;
去掉 k 状态:
显然 i = 0 时 i - 1 是不合法的索引,这是我们没有对 i 的 base case 进⾏处理,可以这样给⼀个特化处理:
实现:
class Solution {
public int maxProfit(int[] prices) {
int n=prices.length;
// 定义
int[][] dp=new int[n][2];
//遍历状态
// base case
dp[0][0]=0;
dp[0][1]=-prices[0];
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], 0 -prices[i]);
}
return dp[n-1][0];
}
}
注意:
base case dp[0][1] 设置为 -prices[0]
昨天没有,今天买入 的情况特殊,为 0-prices[i];
边栏推荐
- Join method in JS
- easyui的combobox默认选中第一个选项
- Jd.com won the highest award for intelligent science and technology in China! Inventory JD system intelligent technology
- Classification of banking business
- 自己学习Cesium的笔记简介
- SQL injection less26 (filter spaces and comments, and use error injection without spaces)
- 09 expr 命令
- The combobox of easyUI selects the first option by default
- leetcode:857. 雇佣 K 名工人的最低成本【分块思考 + 由简单入手】
- JSON字符串转化为JSON对象,获取某个key的值,判断某个key是否存在
猜你喜欢

SQL injection less26 (filter spaces and comments, and use error injection without spaces)

JMeter custom log and log analysis

Summary of debugging stc8a8k64d4 MCU 485 communication

09 expr 命令

Concept and classification of processes

Qt中为工程添加资源文件、给按钮添加图片

07 df 命令

Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click

yolov1

Join method in JS
随机推荐
Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
进程的概念和分类
Finding a new direction for the development of digital retail is the key to ensure that digital retail can enter a new stage of development
[tool] apifox
unity 获取网络时间
Promise me, don't write shit code after reading it..
Altium Designer 22 修改选中元件的层属性
OPPO 自研大规模知识图谱及其在数智工程中的应用
Circular progress bar animation based on cashapelayer and Bezier curve
【Try to Hack】防火墙(一)
『IDEA』IDEA快捷键使用教程
实际权威来自信息优势
Redis distributed lock + Lua atomic operation
Join method in JS
Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
Regular expressions and bypass cases
matlab 激励模型 三角波频谱
: could not determine a constructor for the tag ! RootAdmin
45. Instance segmented labelme dataset to coco dataset and coco dataset to labelme dataset
Xiaobai learns MySQL - derived table