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

1:特殊情况。2:从前到后和从后到前遍历两次数字;3:在遍历的过程中,保存以当前索引为结束点或者開始点的仅仅进行一次买卖的最大收益。4:注意边界结束条件

        int maxProfit(vector<int> &prices)
{
if(prices.size() <= 1)
{
return 0;
} int size = (int)prices.size();
vector<int> leftProfit(size, 0);
leftProfit[0] = 0;
int minValue = prices[0]; int result = 0; for(int i = 1; i < size; i++)
{
if(prices[i] > minValue)
{
leftProfit[i] = (prices[i] - minValue > leftProfit[i-1] ? prices[i] - minValue : leftProfit[i-1]);
}
else
{
minValue = prices[i];
leftProfit[i] = leftProfit[i-1];
}
} result = leftProfit[size-1] > leftProfit[size-2] ? leftProfit[size-2] : leftProfit[size-1];
int maxValue = prices[size -1];
int rightMaxProfit = 0; for(int i = size - 2; i >= 0; i--)
{
if(prices[i] < maxValue)
{
rightMaxProfit = (rightMaxProfit > maxValue - prices[i] ? rightMaxProfit : maxValue - prices[i]);
}
else
{
maxValue = prices[i];
} if(i == 0)
{
result = (result > rightMaxProfit ? result : rightMaxProfit);
}
else
{
result = (result > rightMaxProfit + leftProfit[i-1] ? result : rightMaxProfit + leftProfit[i-1]);
}
} return result;
}

111_leetcode_Best Time to Buy and Sell Stock III的更多相关文章

  1. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

  2. LeetCode 笔记23 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. Best Time to Buy and Sell Stock | & || & III

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

  4. 【leetcode】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 ...

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

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

  7. LeetCode: Best Time to Buy and Sell Stock III 解题报告

    Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...

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

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

随机推荐

  1. 丢手帕问题(环形链表)---Java 待优化

    /** * * @author Administrator * 功能:丢手帕问题 */ package com.litao; public class Demo4 { /** * @param arg ...

  2. 使用Oracle的存储过程批量插入数据

    原文地址:http://www.cnblogs.com/liaoyu/p/oracle-procedure-batch-insert.html 作者:L君还在说之乎者也 最近在工作中,需要使用生成一些 ...

  3. 手势识别官方教程(8)拦截触摸事件,得到触摸的属性如速度,距离等,控制view展开

    onInterceptTouchEvent可在onTouchEvent()前拦截触摸事件, ViewConfiguration得到触摸的属性如速度,距离等, TouchDelegate控制view展开 ...

  4. amaze UI的使用

    1.放置在独立的位置 2.引入核心css与js <link href="{sh::PUB}amaze-ui/css/amazeui.min.css" rel="st ...

  5. WPF——菜单栏及TabControl

    一.先造一个窗体,然后在窗体里面增加菜单栏及原始的TabControl选项卡 <Grid> <Menu> <MenuItem Header="文件" ...

  6. Linux下归档与压缩工具笔记

    tar具体使用笔记 归档工具 tar 语法 功能 选项 常见搭配 压缩工具 bzip2 工具 使用方法 gzip 工具 zip 工具 归档工具 tar tar是一个开源的Linux/Unix中最广泛使 ...

  7. 多线程模式之MasterWorker模式

    多线程模式之MasterWorker模式 Master-Worker模式的核心思想是,系统由两类进程协作工作:Master进程和Worker进程.Master进程负责接收和分配任务,Worker进程负 ...

  8. JSP-讲解(生成java类、静态导入与动态导入)

    一.JSP技术简介 JSP是Java Server Page的缩写,它是Servlet的扩展,它的作用是简化网站的创建和维护. JSP是HTML代码与Java代码的混合体. JSP文件通常以JSP或J ...

  9. 开发工具安装运行bug总结

    如果tomcat出现闪退 在startup.bat--编辑   在文件最后加上 pause  ,再跑一次,可以看到闪退的原因. 一般是环境变量问题,只需要打开starup.bat--编辑,最方件的最上 ...

  10. HDU 1754 I Hate It 线段树 单点更新 区间最大值

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...