Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

分析

首先不压缩空间的写法,写法依然参照stock problem精帖的base case ,分别用一个二维数组去储存sell和buy的状况

const int inf=-999999;
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()<=1)
return 0;
int days=prices.size();
int sell[days+1][3],buy[days+1][3];
for(int i=0;i<3;i++){
sell[0][i]=0;
buy[0][i]=inf;
}
for(int i=0;i<=prices.size();i++){
sell[i][0]=0;
buy[i][0]=inf;
}
for(int i=1;i<=prices.size();i++){
for(int k=1;k<=2;k++){
sell[i][k]=max(sell[i-1][k],buy[i-1][k]+prices[i-1]);
buy[i][k]=max(buy[i-1][k],sell[i-1][k-1]-prices[i-1]);
}
}
return sell[prices.size()][2];
}
};

LeetCode 123. Best Time to Buy and Sell Stock III (stock problem)的更多相关文章

  1. LN : leetcode 123 Best Time to Buy and Sell Stock III

    lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...

  2. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. [leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. leetcode 123. Best Time to Buy and Sell Stock III ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. Leetcode#123 Best Time to Buy and Sell Stock III

    原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...

  7. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  9. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. Linux 入门学习教材

    我大约从两年前开始接触Linux,在那之前工作中用的都是MCU,arm-cortex M系列的. 从单片机转向Linux学习,经历了很多的困难,刚开始都不知道怎么去编译, 网上也没有找到基础的教程,后 ...

  2. MVC、MVP和MVVM的更简单易懂的理解

    本篇转自网络: 一.MVC MVC模式的意思是,软件可以分成三个部分. 视图(View):用户界面. 控制器(Controller):业务逻辑 模型(Model):数据保存 各部分之间的通信方式如下. ...

  3. Ubuntu下如何用命令运行deb安装包

    转载自 WindTaiL的博客 如果ubuntu要安装新软件,已有deb安装包(例如:iptux.deb),但是无法登录到桌面环境.那该怎么安装?答案是:使用dpkg命令. dpkg命令常用格式如下: ...

  4. Service官方教程(8)Bound Service示例之2-跨进程使用Messenger

    Compared to AIDL When you need to perform IPC, using a Messenger for your interface is simpler than ...

  5. 【转载】WebConfigurationManager和ConfigurationManager

    原文链接 今天在写程序时偶然发现有的示例代码中是用WebConfigurationManager获取web.config中的配置信息的,因为之前一直都是用的ConfigurationManager,所 ...

  6. 有关LD_LIBRARY_PATH与ld.so.conf

    我之前写过一篇关于LD_LIBRARY_PATH与gcc/g++ -L的关系的文章,于是我自己用CPACK制作了一个Debian安装包,然后我在/home/.bashrc里添加了export LD_L ...

  7. ural1437

    1437 记忆化 模拟倒水过程 #include <iostream> #include<cstdio> #include<cstring> #include< ...

  8. linux下常用网络操作汇总 专题

    centos 更改主机名,需要更改的几个地方: (1) /etc/sysconfig/network  //更改主机名(2)/etc/hostname  //更改主机名(3) /etc/hosts   ...

  9. AJPFX总结mysql复制表结构,表数据

    1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable; ...

  10. jq星星评分

    html代码 <div class="make_mark"> <h5>请为这次服务打分</h5> <div class="mar ...