题目:

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

题解:

根据题目要求,最多进行两次买卖股票,而且手中不能有2只股票,就是不能连续两次买入操作。

所以,两次交易必须是分布在2各区间内,也就是动作为:买入卖出,买入卖出。

进而,我们可以划分为2个区间[0,i]和[i,len-1],i可以取0~len-1。

那么两次买卖的最大利润为:在两个区间的最大利益和的最大利润。

一次划分的最大利益为:Profit[i] = MaxProfit(区间[0,i]) + MaxProfit(区间[i,len-1]);

最终的最大利润为:MaxProfit(Profit[0], Profit[1], Profit[2], ... , Profit[len-1])。

代码如下:

 1     public int maxProfit(int[] prices) {  
 2         if(prices == null || prices.length <= 1){  
 3             return 0;  
 4         }  
 5         int len = prices.length;  
 6         int maxProfit = 0;  
 7         int min = prices[0];  
 8         int arrayA[] = new int[len];  
 9         
         for(int i=1;i<prices.length;i++){
             min=Math.min(min,prices[i]);
             arrayA[i]=Math.max(arrayA[i-1],prices[i]-min);
         }
         
         int max = prices[len-1];  
         int arrayB[] = new int[len];  
         for(int i = len-2; i >= 0; i--){
             max = Math.max(prices[i],max);
             arrayB[i] = Math.max(max-prices[i],arrayB[i+1]);
         }  
         
         for(int i = 0; i < len; i++){  
             maxProfit = Math.max(maxProfit,arrayA[i] + arrayB[i]);
         }  
         
         return maxProfit;  
     } 

Reference:

http://blog.csdn.net/u013027996/article/details/19414967

Best Time to Buy and Sell Stock III leetcode java的更多相关文章

  1. 123. Best Time to Buy and Sell Stock III ——LeetCode

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

  2. Best Time to Buy and Sell Stock III [LeetCode]

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

  3. Best Time to Buy and Sell Stock II leetcode java

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

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

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

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

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

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

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

随机推荐

  1. [js]事件篇

    一.事件流 1.冒泡事件:从特定的事件到不特定事件依次触发:(由DOM层次的底层依次向上冒泡) (1)示例: <html onclick="add('html<br>')& ...

  2. CSS 显示或隐藏子元素

    很多时候我们仅仅只是想让鼠标移动入某个元素,然后显示出某个元素. 大多数博客的标题或内容都是:使用CSS实现鼠标悬停在一行上,显示某些元素 很遗憾,这是错误的,鼠标悬停后,尽管CSS标准中有定义此种方 ...

  3. 前端安全系列之二:如何防止CSRF攻击?

    背景 随着互联网的高速发展,信息安全问题已经成为企业最为关注的焦点之一,而前端又是引发企业安全问题的高危据点.在移动互联网时代,前端人员除了传统的 XSS.CSRF 等安全问题之外,又时常遭遇网络劫持 ...

  4. 分分钟搞定Python之排序与列表

    排序时程序中用得比较多的方法了.在Python中,最简单的排序方法摸过与使用内置的sorted(list)这个函数了,该函数一一个列表作为参数返回一个新的列表,只不过是把旧列表中的元素排过序了.原列表 ...

  5. bzoj2660: [Beijing wc2012]最多的方案

    题目链接 bzoj2660: [Beijing wc2012]最多的方案 题解 对于一个数的斐波那契数列分解,他的最少项分解是唯一的 我们在拆分成的相临两项之间分解后者,这样形成的方案是最优且不重的 ...

  6. 友好的KVO

    更友好的KVO 前言 观察者模式是大家在开发过程中每个人都要使用的一种设计模式,在iOS的开发流程中,KVO则是这一开发模式的主要实践手段,观察一个属性,当属性值发生变化的就能能够拿到这个属性的新.老 ...

  7. Azure ServiceBus的消息中带有@strin3http//schemas.microsoft.com/2003/10/Serialization/�

    今天碰到一个很讨厌的问题,使用nodejs 接收Azure service bus队列消息的时候,出现了:@strin3http//schemas.microsoft.com/2003/10/Seri ...

  8. 2 Scala基本语法

    1 变量和函数 变量: Scala 有两种变量, val 和 var. val:常量,类似于 Java 里的 final 变量.一旦初始化了, val 就不能再赋值了. va: 如同 Java 里面的 ...

  9. unity热更新

    Unity3D 学习笔记4 —— UGUI+uLua游戏框架 C#Light 和 uLua的对比第二弹 在Unity中使用Lua脚本:语言层和游戏逻辑粘合层处理 Ulua_toLua_基本案例 Uni ...

  10. HTTP请求头和响应头

      这篇文章简单总结一下HTTP请求头和响应头,并举一些web开发中响应头的用例. 1. HTTP请求头 accept:浏览器通过这个头告诉服务器,它所支持的数据类型.如:text/html, ima ...