当前位置:网站首页>Dynamic Programming Problems (End)
Dynamic Programming Problems (End)
2022-07-30 04:37:00 【std i hurt o love】
一、买卖股票的最好时机(二)
解法一:动态规划(推荐)
- 用dp[i][0]表示第i天不持股到该天为止的最大收益,dp[i][1]表示第i天持股,到该天为止的最大收益.
- 第一天不持股,则总收益为0,dp[0][0]=0;第一天持股,则总收益为买股票的花费,此时为负数,dp[0][1]=−prices[0].
- 对于之后的每一天,如果当天不持股,有可能是前面的若干天中卖掉了或是还没买,因此到此为止的总收益和前一天相同,也有可能是当天卖掉股票,我们选择较大的状态dp[i][0]=max(dp[i−1][0],dp[i−1][1]+prices[i]);
- 如果当天持股,可能是前几天买入的还没卖,因此收益与前一天相同,也有可能是当天买入,减去买入的花费,同样是选取最大值:dp[i][1]=max(dp[i−1][1],dp[i−1][0]−prices[i]).
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
//dp[i][0]表示某一天不持股到该天为止的最大收益,dp[i][1]表示某天持股,到该天为止的最大收益
vector<vector<int> > dp(n, vector<int>(2, 0));
//第一天不持股,总收益为0
dp[0][0] = 0;
//第一天持股,总收益为减去该天的股价
dp[0][1] = -prices[0];
//遍历后续每天,状态转移
for(int i = 1; i < n; i++){
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
//最后一天不持股,到该天为止的最大收益
return dp[n - 1][0];
}
};
时间复杂度:O(n),其中n为数组长度,遍历一次数组
空间复杂度:O(n)),Dynamic programming auxiliary arrays are equivalent to two one-dimensional arrays
解法二:贪心
其实我们要想获取最大收益,只需要在低价买入高价卖出就可以了,因为可以买卖多次.利用贪心思想:只要一段区间内价格是递增的,那么这段区间的差值就是我们可以有的收益.
- 遍历数组,只要数组后一个比前一个更大,就可以有收益.
- 将Accumulation of benefits,得到最终结果.

class Solution {
public:
int maxProfit(vector<int>& prices) {
int res = 0;
for(int i = 1; i < prices.size(); i++){
//As long as a certain segment is increasing, there is a profit
if(prices[i - 1] < prices[i])
//Accumulation of benefits
res += prices[i] - prices[i - 1];
}
return res;
}
};
时间复杂度:O(n),其中n为数组长度,遍历一次数组
空间复杂度:O(1),常数级变量,没有使用额外辅助空间
二、买卖股票的最好时机(三)
解法:动态规划
这道题与买卖股票的最好时机(一)的区别在于最多可以买入卖出2次,那实际上相当于它的状态多了几个,对于每天有到此为止的最大收益和持股情况两个状态,持股情况有了5种变化,我们用:
dp[i][0]表示到第i天为止没有买过股票的最大收益
dp[i][1]表示到第i天为止买过一次股票还没有卖出的最大收益
dp[i][2]表示到第i天为止买过一次也卖出过一次股票的最大收益
dp[i][3]表示到第i天为止买过两次只卖出过一次股票的最大收益
dp[i][4]表示到第i天为止买过两次同时也买出过两次股票的最大收益
So use dynamic programming,With the following state transitions
- 与上述提到的题类似,第0天有买入了和没有买两种状态:dp[0][0]=0、dp[0][1]=−prices[0]
- 对于后续的每一天,如果当天还是状态0,则与前一天相同,没有区别;
- 如果当天状态为1,可能是之前买过了或者当天才第一次买入,选取较大值:dp[i][1]=max(dp[i−1][1],dp[i−1][0]−prices[i]);
- 如果当天状态是2,那必须是在1的状态下(已经买入了一次)当天卖出第一次,或者早在之前就卖出只是还没买入第二次,选取较大值:dp[i][2]=max(dp[i−1][2],dp[i−1][1]+prices[i]);
- 如果当天状态是3,那必须是在2的状态下(已经卖出了第一次)当天买入了第二次,或者早在之前就买入了第二次,只是还没卖出,选取较大值:dp[i][3]=max(dp[i−1][3],dp[i−1][2]−prices[i]);
- 如果当天是状态4,那必须是在3的状态下(已经买入了第二次)当天再卖出第二次,或者早在之前就卖出了第二次,选取较大值:dp[i][4]=max(dp[i−1][4],dp[i−1][3]+prices[i]).
- 最后我们还要从0、第一次卖出、第二次卖出中选取最大值,因为有可能没有收益,也有可能只交易一次收益最大.
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
//初始化dp为最小
vector<vector<int> > dp(n, vector<int>(5, -10000));
//第0天不持有状态
dp[0][0] = 0;
//第0天持有股票
dp[0][1] = -prices[0];
//状态转移
for(int i = 1; i < n; i++){
dp[i][0] = dp[i - 1][0];
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
}
//选取最大值,Can only be done once
return max(dp[n - 1][2], max(0, dp[n - 1][4]));
}
};
时间复杂度:O(n),其中n为数组长度,只遍历一次数组
空间复杂度:O(n),Dynamic programming 2D-aided equivalent5个一维数组
边栏推荐
- 权值线段树+线段树分裂/合并+CF1659D
- 共建共享数字世界的根:阿里云打造全面的云原生开源生态
- Usage of EFR32 as sniffer for Zigbee/Thread
- 【MySQL系列】-B+树索引和HASH索引有什么区别
- Introduction to Thymeleaf
- 四、Web开发
- Solve the go environment can not compile exe
- MySql 怎么查出符合条件的最新的数据行?
- golang八股文整理(持续搬运)
- A must see for software testers!Database knowledge MySQL query statement Daquan
猜你喜欢

验证addShutdownHook钩子生效

@WebServlet注解(Servlet注解)

MySQL String Concatenation - Various String Concatenation Practical Cases

2.6基数排序(桶排序)

swagger使用教程——快速使用swagger

Install MySQL Database on Kylin V10 Operating System

Android Studio implements login registration - source code (connecting to MySql database)

KubeMeet Registration | The complete agenda of the "Edge Native" Online Technology Salon has been announced!

Xiamen SenseCore Technology MC3172(1): Introduction and Environment Construction

Thymeleaf简介
随机推荐
2021山东省网络搭建与应用赛项试题
【翻译】Envoy Fundamentals,这是一个培训课程,使人们能够更快地采用Envoy Proxy。...
DAY17: weak password detection and test
[C language] Program environment and preprocessing
Usage of EFR32 as sniffer for Zigbee/Thread
05全局配置文件application.properties详解
The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Partial WP (10)
error: The following untracked working tree files would be overwritten by
Android Studio 实现登录注册-源代码 (连接MySql数据库)
Simple experiment with BGP
DAY17, CSRF vulnerability
七、自定义配置
QT(39)-vs development qt program prompts that the source file cannot be opened
2.4 hill sorting
labelme的使用技巧
SSM框架简单介绍
Excellent MySQL interview questions, 99% must ask in preparation for August!I can't pass the interview
Shanxi group (enterprises) in the second network security skills competition part problem WP (7)
The VUX Datetime component compute-days-function dynamically sets the date list
The Azure developer news 丨 memorabilia in July