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. 实现strcmp功能

    判断两个字符串的大小 #include <stdio.h> int my_strcmp(const char *str1,const char *str2) { //判断两个字符串是否为空 ...

  2. python的Template使用指南

    本文主要讲解了python中Template使用方法以及使用技巧,非常实用,有需要的朋友可以参考下: Template无疑是一个好东西,可以将字符串的格式固定下来,重复利用.同时Template也可以 ...

  3. 题解报告:hdu 2093 考试排名

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2093 Problem Description C++编程考试使用的实时提交系统,具有即时获得成绩排名的 ...

  4. .NET下集中实现AOP编程的框架

    一.Castle 使用这个框架呢,首先是需要安装NuGet包. 先建立一个控制台项目,然后在NuGet中搜索Castle.Windsor,不出意外的话应该能找到如下的包 然后安装,会自动的安装包Cas ...

  5. synchronized(5)修饰语句块之:synchronized(XXX.class)

    synchronized(XXX.class)有两种写法 synchronized(XXX.class)或者synchronized(obj.getClass()) Class也是一个类xxx.cla ...

  6. Android SQLite(1)简单示例-增,删,改,查

    1.主要核心类,Sqlite编程要继承SQLiteOpenHelper import android.content.Context; import android.database.sqlite.S ...

  7. 多功能Markdown编辑器MarkdownPad 2的下载、安装和初步使用步骤(图文详解)(博主推荐)

    不多说,直接上干货!   MarkdownPad 是什么? 一.MarkdownPad 2的下载 http://markdownpad.com/download/markdownpad2-setup. ...

  8. [转]在ASP.NET MVC3中使用EFCodeFirst 1.0

    本文转自:http://kb.cnblogs.com/page/97003/ 作者: NinoFocus  来源: 博客园  发布时间: 2011-04-12 10:41  阅读: 11971 次   ...

  9. 外文翻译 《How we decide》赛场上的四分卫 第二节

    本书导言翻译 本章第一节 "决定是如何做出来的",关于意识最神秘的问题之一.尽管我们时刻做着决定,但是我们没有感觉到大脑内部的一系列有关进程.NFL球探挑选候选球员的评分表中,决策 ...

  10. MySQL基础、索引、查询优化等考察点

    MySQL基础 MySQL数据类型 整数类型 TINYINT. SMALLINT. MEDIUMINT. INT. BIGINT 属性:UNSIGNED 长度:可以为整数类型指定宽度,例如:INT(1 ...