122. Best Time to Buy and Sell Stock II

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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

分析:主要是寻找数组中的递减子数组,及递增子数组(或者可以认为是极值点),然后求和即可。

class Solution {
public:
int maxProfit(vector<int>& prices) {
int size = prices.size();
if (size <= )
return ;
int index = ;
int profit = ;
while(index < size)
{
int buy,sell;
while(index+ < size && prices[index] > prices[index+]) index++;
buy = index;
while(index+ < size && prices[index]< prices[index+]) index++;
sell = index;
profit += prices[sell] - prices[buy];
index++;
}
return profit;
}
};

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

动态规划,preprofit保存i之前交易获得的收益。postprofit保存i之后交易获得的收益。

只能进行两次交易,在第0-i天,我进行一次交易,在第i天后我进行一次交易。

  • preprofit保存,从开始那天到第i天,如果我进行交易的最大值,从max{preprofit[i-1],price[i]-curmin}获得。

  从前往后扫:因为我要获得arr[0,..i](i++)包含i的最大收益:

          1.要找到0-i的最小值,curmin需要更新

          2.最大收益即之前的preprofit和当前price-curmin作对比。

  • postprofit保存,从第i天到最后一天,如果我进行交易的最大值,从max{postprofit[i-1],curmax-postprofit[i]}获得。

  从后往前扫描:因为我要获得arr[i+1……n](i++)的最大收益

          1.要找到i+1~n的最大值,如果不是从后往前扫,而是从前往后扫,curmax保存的是0-i的最大值。而我们要找的是i+1~n的最大值。

          2.最大收益即之后postprofit和当前的curmax-price做对比。

整个算法是这样的:是把整个数组分两段Max( MaxProfitOn[0,i] + MaxProfitOn[i+1, n-1] ),分别找最大,看看他们的和在如何分的时候才能达到全局最大。

  

最后遍历preprofit和postprofit数组,max{preprofit[i]+postprofit[i]}既是最大的可能收益。

class Solution {
public:
int maxProfit(vector<int>& prices) {
int size=prices.size();
if (size<)
return ;
int curmin=prices[],curmax=prices[size-];
vector<int> preProfit(size,);
vector<int> postProfit(size,);
for(int i=;i<size;i++){
curmin=min(curmin,prices[i]);
preProfit[i]=max(prices[i]-curmin,preProfit[i-]);
}
for (int j = size-;j>=; --j){
curmax=max(curmax,prices[j]);
postProfit[j]=max(postProfit[j+],curmax-prices[j]); }
int retProfit=;
for(int m=;m<size;m++){
if(retProfit<(preProfit[m]+postProfit[m]))
retProfit=preProfit[m]+postProfit[m];
}
return retProfit;
}
};

[leetcode]股票题型123的更多相关文章

  1. [leetcode] 股票问题

    参考文章: [1] 团灭 LeetCode 股票买卖问题 [2] Most consistent ways of dealing with the series of stock problems 其 ...

  2. leetcode 新题型----SQL,shell,system design

    leetcode 主要是一个针对北美的coder人群找工作的代码练习网站,我在2015年初次接触这个网站的时候,总共只有200多道题目,是一个类似acm 的a题网站.这些年变化越来越大,主要是因为找工 ...

  3. leetcode股票问题方法收集 转载自微信公众号labuladong

    一.穷举框架首先,还是一样的思路:如何穷举?这里的穷举思路和上篇文章递归的思想不太一样. 递归其实是符合我们思考的逻辑的,一步步推进,遇到无法解决的就丢给递归,一不小心就做出来了,可读性还很好.缺点就 ...

  4. LeetCode矩阵题型

    以三角形遍历矩阵 ; i < matrix.size(); ++i) { ; j < matrix[i].size(); ++j) swap(matrix[i][j], matrix[j] ...

  5. leetcode 121 122 123 . Best Time to Buy and Sell Stock

    121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...

  6. leetcode 股票系列

    五道股票题总结: 121 买卖股票的最佳时机 122 买卖股票的最佳时机 124 买卖股票的最佳时机4 309  最佳股票买卖含冷冻期 714 买卖股票的最佳时机含有手续费 121 买卖股票的最佳时机 ...

  7. 5分钟了解二叉树之LeetCode里的二叉树

    有读者反馈,现在谁不是为了找工作才学的数据结构,确实很有道理,是我肤浅了.所以为了满足大家的需求,这里总结下LeetCode里的数据结构.对于我们这种职场老人来说,刷LeetCode会遇到个很尴尬的问 ...

  8. java程序员学习路线阶段总结20190903

    算法:锻炼写代码的逻辑 刷题位置:leetcode 书籍:小灰漫画算法 leecode使用方法: 转载自http://blog.csdn.net/tostq 又到了一年毕业就业季了,三年前的校招季我逃 ...

  9. Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)

    Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...

随机推荐

  1. 知识点:MySQL表名不区分大小写的设置方法

    在用centox安装mysql后,把项目的数据库移植了过去,发现一些表的数据查不到,排查了一下问题,最后发现是表名的大小写不一致造成的. mysql在windows系统下安装好后,默认是对表名大小写不 ...

  2. JDK8 Java 中遇到null 和为空的情况,使用Optional来解决。

    空指针是我们最常见也最讨厌的异常,写过 Java 程序的同学,一般都遇到过 NullPointerException :) 初识null 详细可以参考[jdk 1.6 Java.lang.Null.P ...

  3. 【TensorFlow学习笔记 】name_socpe variable_scope

    [引言]TensorFlow中的命名域是非常重要的概念,涉及到参数共享,方便命名参数管理,定义图结构 本文主要介绍name_scope 和 variable_scope,slim包中的arg_scop ...

  4. CentOS 关闭图形用户界面

    1 centos 7以下版本 vim /etc/inittab :initdefault: #改为 :initdefault: 2. centos 7.x版本 rm -f /etc/systemd/s ...

  5. Html5——canvas标签使用

    canvas 拥有多种绘制路径.矩形.圆形.字符以及添加图像的方法. canvas 元素本身是没有绘图能力的.所有的绘制工作必须在 JavaScript 内部完成 <script type=&q ...

  6. 16 Linux系统的文件压缩、解压与归档

    这一节的内容,我们详细介绍下Linux的文件压缩.解压缩与文件归档的内容,也就是tar.gzip.bzip2.xz等命令的内容: 压缩(compress)与解压缩(uncompress) Linux系 ...

  7. day6--面向对象初识

    一面向过程与面向对象 面向过程: 流水线式的思维,顺着流程进行下去,类似于代码的堆叠,重视步骤 优点:不需要考虑太多东西,想到一个功能就写一个功能,堆叠代码 缺点:过程往往是先后执行的,要想变换功能或 ...

  8. vmware三种网络模式:桥接模式|仅主机|NAT模式

    VMware 网络模式 1. Bridged(桥接)桥接模式下,宿主机物理网卡(主机网卡)和虚拟网卡通过 VMnet0 虚拟交换机进行桥接,物理网卡和虚拟网卡在拓扑图上处于同等地位,物理网卡和虚拟网卡 ...

  9. 作为sort()方法的参数的比较函数(高程三第五章)

    <script> var nums = [0,1,5,10,15]; var nums2 = nums; nums.sort(); console.log(nums);//0,1,10,1 ...

  10. NDK samples以及部分博客

    谷歌的ndk samples Android NDK samples with Android Studio http://developer.android.com/ndk 引用C/C++文件请参考 ...