@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. 呵呵哒,LNMP下通过fread方式下载文件时,中文名称文件找不到文件

    哎,整整折腾一个下午. 本来好好的,thinkphp 自动的uniq方式保存的文件名,非要使用原文件名,真心蛋疼~~ 然后就只好写个脚本 把原来的所有文件都重新命名一下 - - 然后把数据库对应字段也 ...

  2. oracle数据库冷备中的手工备份和恢复

    我的操作系统是red hat5.5 32位系统oracle11g 以我的系统为例: 冷备状态下,数据库必须是关闭的,但是我们现在要做一个实验,在开库的状态下分别查询出: 1.show paramete ...

  3. 机器学习,安装python的支持包

    windows10,64位: 以下命令行安装均在python目录下,对应的whl文件也被我拷贝到python目录下: http://www.lfd.uci.edu/~gohlke/pythonlibs ...

  4. js函数整合队列顺序执行插件

    前言 在日常开发中,也许我们会遇到这样的一个问题.我们利用[发布订阅模式](如果不了解的可以直接访问此链接www.cnblogs.com/xiaoxiaokun- )去执行[发布]事件时,遇到函数内部 ...

  5. linux 内存 大于 jvm xmx

    文章来源: http://www.cnblogs.com/guozp/p/7845605.html 1.虽然你jvm参数设置了-Xms6g -Xmx6g,但操作系统不并会马上分配6G的物理内存,而是确 ...

  6. 深入理解CSS盒模型

    如果你在面试的时候面试官让你谈谈对盒模型的理解,你是不是不知从何谈起.这种看似简单的题其实是最不好答的. 下面本文章将会从以下几个方面谈谈盒模型. 基本概念:标准模型 和IE模型 CSS如何设置这两种 ...

  7. vim7.3中文乱码问题

    在测试机安装vim7.3之后编辑中文文本出现乱码问题. vim在编译安装的时候: ./configure --enable-gdb --enable-multibyte --enable-cscope ...

  8. js 图片转换为base64 (2)

    <input type="file" id="testUpload"> <img src="" id="img& ...

  9. Gluon炼丹(Kaggle 120种狗分类,迁移学习加双模型融合)

    这是在kaggle上的一个练习比赛,使用的是ImageNet数据集的子集. 注意,mxnet版本要高于0.12.1b2017112. 下载数据集. train.zip test.zip labels ...

  10. C#常见错误解决方法

    1.能提供Visual Studio开发工具包吗? 解决方法: Visual Studio 2017开发环境下载地址: https://www.visualstudio.com/zh-hans/dow ...