题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/

题目大意:与122题类似,只是这里要求买卖次数只能有两次,计算两次总共的最大利润值。

法一(超时):计算某一天之前的最大利润值与某一天之后的最大利润值,然后用for循环遍历得到最大值,时间复杂度是o(n^2),代码如下:

 int res = 0;
for(int i = 0; i < prices.length; i++) {
int maxPre = 0;
int min = Integer.MAX_VALUE;
//0 to i-1
for(int j = 0; j < i; j++) {
if(prices[j] < min) {
min = prices[j];
}
else if(prices[j] - min > maxPre){
maxPre = prices[j] - min;
}
}
//i to n-1
int maxPost = 0;
min = Integer.MAX_VALUE;
for(int j = i; j < prices.length; j++) {
if(prices[j] < min) {
min = prices[j];
}
else if(prices[j] - min > maxPost){
maxPost = prices[j] - min;
}
} if(maxPre + maxPost > res) {
res = maxPre + maxPost;
}
}
return res;

法二(借鉴):动态规划,利用法一的思路,只是这里不用for循环嵌套遍历得到最大值,而是用两个数组分别记录某一天之前的最大利润值和某一天之后的最大利润值,然后再另开一个for循环,计算比较得到最大值。这里有两个动规公式:

1.数组记录最大利润:

  1)某一天之前:preProfit[i] = max(preProfit[i - 1], prices[i] - min);

  2)某一天之后:postProfit[i] = max(postProfit[i + 1], max - prices[i]);

2.for循环计算得到最大值:res = max(res, preProfit[i] + postProfit[i]);

代码如下(耗时1ms):

         int length = prices.length;
int[] preProfit = new int[length];
int[] postProfit = new int[length];
//从前往后找,0 to i,用数组记录每一天i之前所能获得的最大利润,计算过程与121题类似
int minPrice = Integer.MAX_VALUE;
int max = 0;
for(int i = 0; i < length; i++) {
if(prices[i] < minPrice) {
minPrice = prices[i];
}
else if(prices[i] - minPrice > max) {
max = prices[i] - minPrice;
}
preProfit[i] = max;
}
//从后往前,i to n-1,用数组记录每一天i之后所能获得的最大利润
//注意:从后往前找的时候,应该记录当前位置之后的最大价值,然后将当前位置的价值与最大价值进行比较
int maxPrice = Integer.MIN_VALUE;
max = 0;
for(int i = length - 1; i >= 0; i--) {
if(prices[i] > maxPrice) {
maxPrice = prices[i];
}
else if(maxPrice - prices[i] > max) {
max = maxPrice - prices[i];
}
postProfit[i] = max;
} int res = 0;
for(int i = 0; i < length; i++) {
if(preProfit[i] + postProfit[i] > res) {
res = preProfit[i] + postProfit[i];
}
}
return res;

法三(借鉴):参考:http://blog.csdn.net/u012501459/article/details/46514309解法二。

123.Best Time to Buy and Sell Stock III---dp的更多相关文章

  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. LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法

    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

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

  4. [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 ...

  5. 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  6. 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 a ...

  7. LeetCode OJ 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 ...

  8. 123. Best Time to Buy and Sell Stock III ——LeetCode

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

  9. [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 ...

  10. 123. Best Time to Buy and Sell Stock III (Array; DP)

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

随机推荐

  1. HDU——1573 X问题

    又来一发水题. 解同余方程而已,用类似于剩余定理的方法就O了. 直接上代码:(注意要判断是否有解这种情况) #include <iostream> #include <cstdio& ...

  2. hadoop跑第一个实例过程

    第一次跑hadoop实例,中间经过了不少弯路,特此记录下来: 第一步:建立一个maven过程,pom.xml文件:(打包为jar包) <dependency> <groupId> ...

  3. java中的error该不该捕获

    写java程序时,通常会被提示捕获异常,而又有一些异常是不需要强制捕获的,这是一个被说烂了的话题.像我一样从其他语言转过来的人确实有点迷惑,那我以我的理解重新解释一遍吧. 异常的基类是Exceptio ...

  4. 【Java】JAVA开发人员常见环境工具安装

    1.安装配置JDK1.7:jdk-7u45-windows-x64.exe,环境变量配置:JAVA_HOME---[F:\1024\jdk1.7],CLASSPATH---[.;%JAVA_HOME% ...

  5. BZOJ4976 宝石镶嵌(动态规划)

    显然被留下的宝石应该贡献至少一位,否则就可以扔掉.所以如果n-k>=logw,直接输出所有数的or.现在n变得和k同阶了.于是设f[i][j]为前i个数or为j时至少选几个数,转移显然.当然可以 ...

  6. JS中数组和字符串具有的方法,以及substring,substr和slice的用法与区别

     String 对象属性 属性 描述 constructor 对创建该对象的函数的引用 length 字符串的长度 prototype 允许您向对象添加属性和方法 String 对象方法 方法 描述 ...

  7. VSS2005清除管理员密码

    1.下载工具ultraedit 2.登录到服务器,找到VSS库文件夹,data\um.dat 3.复制到自己桌面,用ultraedit打开,进入 引用内容 00000080h: 55 55 03 29 ...

  8. mac os 启动服务命令 launchctl

    参考苹果开发者网址 https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Ch ...

  9. mysql5.7主从(Master/Slave)同步配置

    环境: mysql版本都是5.7(以前的版本配置可能不一样) 主(Master) windows:192.168.0.68 从(Slave) centos7:192.168.0.4 基本环境配置: 要 ...

  10. 装饰器--decorator1

    装饰器 一.定义 1.装饰器:本质是函数 2.功能:用来装饰其他函数,为其他函数添加附加功能 二.原则 1.不能修改被装饰函数的源代码 2.不能修改被装饰函数的调用方式 三.实现装饰器 1.函数 即 ...