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. centos mysql数据库忘记密码修改

    1.vim /etc/my.cnf 2.在[mysqld]中添加 skip-grant-tables 例如: [mysqld]skip-grant-tablesdatadir=/var/lib/mys ...

  2. 命名管道实现进程间通信--石头、剪刀、布游戏 分类: linux 2014-06-01 22:50 467人阅读 评论(0) 收藏

    下面这个程序利用命名管道实现进程间通信,模拟石头剪刀布游戏. 主进程为裁判进程,两个子进程为选手进程.裁判与选手间各建立一个命名管道. 进行100次出招,最后给出游戏胜负. #include < ...

  3. GUI初步和frame&panel

    java的话这个GUI其实不是什么重点,但我们也要学习,重点是学习这种图形编程的思路. java里面对于图形的一些类都封装在了AWT和它的一些子包里.AWT(抽象窗口开发包)            当 ...

  4. LD_LIBRARY_PATH与-L的关系以及延伸

    最近跟同学讨论c++在编译时g++ -L.. 和LD_LIBRARY_PATH的问题,今天在做一个东西的时候发现,我对这两个东西的理解是错误的,经过一番研究,写下我对这些东西的想法,如果有不对的地方, ...

  5. IIS7.0中Process打开cmd程序出现问题

    本人在VS中用Process打开cmd程序,并传入参数,转换图片,运行成功! 但是放入IIS7.0中,Process打不开cmd程序,程序直接运行过去,无结果,无报错! 解决方案: 将IIS里面你程序 ...

  6. AJPFX简述可变参数概述和使用

    A:可变参数概述 定义方法的时候不知道该定义多少个参数 B:格式 修饰符 返回值类型 方法名(数据类型… 变量名){} C:注意事项: 这里的变量其实是一个数组 如果一个方法有可变参数,并且有多个参数 ...

  7. poj3040 Allowance

    思路: 贪心. 看了题解说是 先把面值从大到小排序然后从头往尾扫,只要不超额,能取多少去多少然后如果还有剩余,就从尾往头扫,尽量取,让他恰好超额 不过并不懂证明. 实现: #include <i ...

  8. (2)《Head First HTML与CSS》学习笔记---img与基于标准的HTML5

    1.浏览器处理图像的过程: 1.服务器获取文件,显示出文本结构,以及预留默认的大小给<img>(如果该<img>有width-1值和height-1值,则根据这个值提前设好页面 ...

  9. TabLayout+ViewPager实现标签卡效果

    转载请注明原文地址:http://www.cnblogs.com/yanyojun/p/8082391.html 代码已经上传至Github:https://github.com/YanYoJun/V ...

  10. Toast解析

    课程Demo public class MainActivity extends AppCompatActivity { private Button bt1; private Button bt2; ...