Say you have an array for which the i th 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).

题意:最多两次买卖,而且下一次必须在上一次结束之后。动态规划

方法一:

常规思路是,将整个数组分为两个段,使前半段获得的最大利润加上后半段获得的最大利润的和最大。这就涉及到一个如何分段的问题了?我们可以遍历一遍数组,以每个元素为分结点(分界点,即某天可以先卖出第一次的,再买入第二的),找到和最大的情况。所以原问题就如何分别求得前后两段的最大利润了,而求某一段的最大利润,可参考Best time to buy and sell stock。因为前一段的终点和后一段的起点不确定,若是从左往右以每个点作为元素遍历一次,求出对应的最大利润,这样就要做两次遍历n+ n-1+ n-2...0,时间复杂度就为O(n^2),变成暴力解法。这里提供的思路是,将数组正、反个遍历一次,将各个点的最大利润分别存入两个数组中,这样最后只要以每个元素为界点再遍历一次,3*n,时间复杂度为O(n)。值得注意的是,反向遍历时,不是求右边的最小值,应该为最大值,应为要先买后卖,数组从左到右的方向为时间轴方向,所以反向遍历时,小值在左边,大值在右边。代码如下:

 class Solution {
public:
int maxProfit(vector<int> &prices)
{
int len=prices.size();
if(len==) return ; int profit=; //正向
vector<int> forTrav(len,); //int *forTrav=new int[len];
int lMin=prices[];
int curAns=;
for(int i=;i<len;++i)
{
lMin=min(lMin,prices[i]);
curAns=max(curAns,prices[i]-lMin);
forTrav[i]=curAns;
} //反向
vector<int> resTrav(len,);
int rMax=prices[len-];
curAns=;
for(int i=len-;i>=;--i)
{
rMax=max(rMax,prices[i]);
curAns=max(curAns,rMax-prices[i]);
resTrav[i]=curAns;
} for(int i=;i<len;++i)
{
if(profit<forTrav[i]+resTrav[i])
profit=forTrav[i]+resTrav[i];
} return profit;
}
};

方法二:给出了至多K次的交易次数

参见:Code GanderGrandyang的博客。

[Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机的更多相关文章

  1. [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机

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

  2. 123 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III

    假设你有一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易.注意:你不可同时参与多笔交易(你必须在再次购买前出售掉之前的股票).详见: ...

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

  4. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

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

  5. 122 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II

    假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票) ...

  6. 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV

    假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...

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

  8. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

    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: Best Time to Buy and Sell Stock III [123]

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

随机推荐

  1. IDEA项目启动报Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed"

    启动报错: Unable to open debugger port (127.0.0.1:51554): java.net.SocketException "socket closed&q ...

  2. STL 一些常用的STL函数(持续更新

    先说一下  一边要用到算法的东西一般要加#include<algorithm>头文件 一.栈和队列 1 栈 :一种线性表 特点  后进先出 头文件  #include<stack&g ...

  3. CSS3新特性回顾

    CSS3 介绍 开始实例 新特征简介 强大的CSS选择器 抛弃图片的视觉效果 盒模型变化(多列布局和弹性盒模型) 阴影效果 Web字体和web Font 图标 CSS33过渡与动画交互效果 媒体查询 ...

  4. Django中的select_related与prefetch_related

      Django是一个基于Python的网站开发框架,一个很重要的特点就是Battery Included,简单来说就是包含了常规开发中所需要的一切东西,包括但不限于完整的ORM模型.中间件.会话处理 ...

  5. windows 系统禁止使用 U 盘的方法

    windows 系统禁止使用 U 盘的方法 最简单的办法: 注册表 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentCntrolSet\Services\USBSTOR] 将名为 ...

  6. Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正)

    Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正) 置顶 2017年12月08日 11:19:11 阅读数:20277 官方原文: https://docs.djangoprojec ...

  7. 【算法】 string 转 int

    [算法] string 转 int 遇到的一道面试题, 当时只写了个思路, 现给出具体实现 ,算是一种比较笨的实现方式 public class StringToInt { /// <summa ...

  8. 【APUE】Chapter14 Advanced I/O

    14.1 Introduction 这一章介绍的内容主要有nonblocking I/O, record locking, I/O multiplexing, asynchronous I/O, th ...

  9. 简单工具 & 杂技

    图片压缩: 腾讯智图(http://zhitu.isux.us/) 手机的所有尺寸大小规范: http://screensiz.es/phone 需求: 移动端宽高一致的盒子(因为移动端屏幕宽度不一样 ...

  10. 小议Android多进程以致Application多次初始化

    最近遇到一个bug,当应用加了多进程后,比如总共进程数为N,会出现在`startService()`时`onStartCommand()`方法会被重复调用`(N-1)`次的奇怪现象. ***## 祸起 ...