@requires_authorization
@author johnsondu
@create_time 2015.7.22 19:04
@url [Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)
/************************
* @description: dynamic programming.
* 从前后分别求出当前所能够得到的最大值,最后相加而得
* 详细操作是,设置一个最小值,然后不断更新最小值。然后不断更新最大值。 前后反向累加求得最大值
* @time_complexity: O(n)
* @space_complexity: O(n)
************************/
class Solution {
public:
int maxProfit(vector<int>& prices) {
const int len = prices.size();
if(len < 2) return 0;
int maxFromHead[len];
maxFromHead[0] = 0;
int minprice = prices[0], maxprofit = 0;
for(int i = 1; i < len; i ++) {
minprice = min(prices[i-1], minprice);
if(maxprofit < prices[i] - minprice)
maxprofit = prices[i] - minprice;
maxFromHead[i] = maxprofit;
}
int maxprice = prices[len-1];
int res = maxFromHead[len-1];
maxprofit = 0;
for(int i = len-2; i >= 0; i --) {
maxprice = max(maxprice, prices[i+1]);
if(maxprofit < maxprice - prices[i])
maxprofit = maxprice - prices[i];
if(res < maxFromHead[i] + maxprofit)
res = maxFromHead[i] + maxprofit;
}
return res;
}
};
@requires_authorization
@author johnsondu
@create_time 2015.7.22 19:04
@url [Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)
/************************
* @description: dynamic programming.
* 超时做法:从1開始。分成前后两段,最后求得前后两段的最大值
* @time_complexity: O(n^2)
* @space_complexity: O(n)
************************/
class Solution {
public:
int maxProfit(vector<int>& prices) {
int p_size = prices.size();
if(p_size < 2) return 0;
if(p_size < 3) {
return prices[1] - prices[0] > 0 ? prices[1] - prices[0]: 0;
} int ans = 0;
for(int i = 1; i < p_size; i ++) {
vector<int> dp1, dp2;
for(int j = 1; j <= i; j ++) {
dp1.push_back(prices[i] - prices[i-1]);
}
for(int j = i + 1; j < p_size; j ++) {
dp2.push_back(prices[j] - prices[j-1]);
} int dp1_size = dp1.size();
int ans1 = 0;
int cur = 0;
for(int j = 0; j < dp1_size; j ++) {
cur = cur + dp1[j];
if(cur < 0) {
cur = 0;
continue;
}
if(cur > ans1) ans1 = cur;
} int dp2_size = dp2.size();
int ans2 = 0;
cur = 0;
for(int j = 0; j < dp2_size; j ++) {
cur = cur + dp2[j];
if(cur < 0) {
cur = 0;
continue;
}
if(cur > ans2) ans2 = cur;
}
ans = max(ans, ans1 + ans2);
}
return ans;
}
};

【leetcode】123. Best Time to Buy and Sell Stock III的更多相关文章

  1. 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【刷题-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 ...

  3. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  5. 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 ...

  6. 【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee

    题目如下: Your are given an array of integers prices, for which the i-th element is the price of a given ...

  7. 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...

  8. 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

随机推荐

  1. spring boot hello and docker

    主要是想试下spring boot运行在docker里的感觉, 小试牛刀   :) 这是原文,参考一下:  https://spring.io/guides/gs/spring-boot-docker ...

  2. css伪类的说明以及使用(css事件)

    CSS伪类的使用(css事件) 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/7670959.html 之前有开发开发App的时候,有同事问我那个列表的条目按下 ...

  3. 30.Linux-RTC驱动分析及使用

    linux中的rtc驱动位于drivers/rtc下,里面包含了许多开发平台的RTC驱动,我们这里是以S3C24xx为主,所以它的RTC驱动为rtc-s3c.c 1.进入./drivers/rtc/r ...

  4. C# TabControl标签的隐藏

    当你想要隐藏的时候 if (this.tabMain.TabPages[ "tabpageThePage "] != null) { this.tabMain.TabPages.R ...

  5. javascript正则多次调用test 结果交替出现

    现在需要一个正则验证小数点后保留一到三位数,小数点前只能两位或一位整数的这么一个数. 正则如: var reg = /^\d{,}\.\d{,}$/g; 验证如下: 因为我们用1.23符合规则的数据去 ...

  6. [转载] 2 分钟读懂大数据框架 Hadoop 和 Spark 的异同

    转载自https://www.oschina.net/news/73939/hadoop-spark-%20difference 谈到大数据,相信大家对Hadoop和Apache Spark这两个名字 ...

  7. python基础(三)----字符编码以及文件处理

      字符编码与文件处理 一.字符编码 由字符翻译成二进制数字的过程   字符--------(翻译过程)------->数字   这个过程实际就是一个字符如何对应一个特定数字的标准,这个标准称之 ...

  8. Android OOM异常解决方案

    一,什么是OOM异常: OOM(out of Memory)即内存溢出异常,也就是说内存占有量超过了VM所分配的最大,导致应用程序异常终止: 二,为什么会产生OOM异常呢? OOM异常是Android ...

  9. spring aop使用

    最近做一个数据库分离的功能,其中用到了spring aop,主要思路就是在service层的方法执行前根据注解(当然也可以根据方法名称,如果方法名称写的比较统一的话)来判断具体使用哪个库.所以想着再回 ...

  10. overflow-x: scroll;横向滑动详细讲解

    overflow-x: scroll;横向滑动(移动端使用详解) css3 , ie8以上 <!DOCTYPE html> <html lang="en"> ...