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. 矩阵快速幂/矩阵加速线性数列 By cellur925

    讲快速幂的时候就提到矩阵快速幂了啊,知道是个好东西,但是因为当时太蒟(现在依然)没听懂.现在把它补上. 一.矩阵快速幂 首先我们来说说矩阵.在计算机中,矩阵通常都是用二维数组来存的.矩阵加减法比较简单 ...

  2. Six degrees of Kevin Bacon

    转自:https://blog.csdn.net/a17865569022/article/details/78766867 Input* Line 1: Two space-separated in ...

  3. Taro 采坑日常

    组件事件传参只能在类作用域下的确切引用(this.handleXX || this.props.handleXX),或使用 bind. 组件中点击事件如下 // 组件 <AtListItem k ...

  4. bootstrap 弹出框 另类运用

    下面是我在做一个简单登录页面时,应用boostrap弹出框,通过调节做成警示框的过程,前后经过了一番波折.因为摸索过程十分有趣,最后也是成功的,使用弹出框做除了警示框的效果,下面我们来看一下吧. 首先 ...

  5. Hdu 5336 XYZ and Drops (bfs 模拟)

    题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...

  6. 题解报告:CODE[VS] 1082 线段树练习3(区间修改+区间查询)

    题目描述 Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. 输入描述 Input Description 第一行一个正整数n,接下 ...

  7. Linux oraenv Tips

    Linux for the Oracle DBA -Customizing the Oracle User's Environment There are many ways to customize ...

  8. AJPFX关于this用法和注意事项

    this:代表对象.就是所在函数所属对象的引用.哪个对象调用了this所在的函数,this就代表哪个对象,就是哪个对象的引用.开发时在定义功能时,如果该功能内部使用到了调用该功能的对象,这时就用thi ...

  9. ie浏览器和火狐浏览器对对容器宽度定义的差异

    首先我们说说firefox和IE对CSS的宽度显示有什么不同: 其实CSS ’width’ 指的是标准CSS中所指的width的宽度,在firefox中的宽度就是这个宽度.它只包含容器中内容的宽度.而 ...

  10. dll、lib(动态链接库、静态链接库)的区别

    1.dll:dynamic link library: lib:static link library. 2.windows系统中,许多app并不是仅由一个完整的exe构成,而是按功能分成了若干部分, ...