题目:

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. poi类包对比

  2. zoj-1610线段树刷题

    title: zoj-1610线段树刷题 date: 2018-10-16 16:49:47 tags: acm 刷题 categories: ACM-线段树 概述 这道题是一道简单的线段树区间染色问 ...

  3. Initramfs 原理和实践

    Linux系统启动时使用initramfs (initram file system), initramfs可以在启动早期提供一个用户态环境,借助它可以完成一些内核在启动阶段不易完成的工作.当然ini ...

  4. 机器学习之路:python 多项式特征生成PolynomialFeatures 欠拟合与过拟合

    分享一下 线性回归中 欠拟合 和 过拟合 是怎么回事~为了解决欠拟合的情 经常要提高线性的次数建立模型拟合曲线, 次数过高会导致过拟合,次数不够会欠拟合.再建立高次函数时候,要利用多项式特征生成器 生 ...

  5. 4144: [AMPPZ2014]Petrol (多源最短路+最小生成树+启发式合并)

    4144: [AMPPZ2014]Petrol Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 752  Solved: 298[Submit][Sta ...

  6. C# 中判断字符串是否包含另一段字符串,请使用 Contains

    使用:Contains 比 IndexOf 的性能提高很多. 因为 Contains 是判断某个字符串是否在另外一个字符串中,而IndexOf需要返回下标值.

  7. hdu 1429 bfs+状压

    题意:这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开始 Ignatius被关在(sx,sy)的位置,离开地牢的门 ...

  8. TSQL update 简单应用小总结

    UPDATE 有两种基本的格式.一种是用静态数据来修改表,另一种是用其他表中的数据来修改表.下面是第一种格式: UPDATE #famousjaycees SET jc = 'Jhony cash', ...

  9. mongoDB系列之(三):mongoDB 分片

    1. monogDB的分片(Sharding) 分片是mongoDB针对TB级别以上的数据量,采用的一种数据存储方式. mongoDB采用将集合进行拆分,然后将拆分的数据均摊到几个mongoDB实例上 ...

  10. PHP获取数组中奇偶数

    获取PHP数组中的奇偶数,可通过数组过滤函数array_filter(),看定义:该函数把输入数组中的每个键值传给回调函数.如果回调函数返回 true,则把输入数组中的当前键值返回结果数组中.数组键名 ...