Question

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

Solution

This problem can be solved by "divide and conquer". We can use left[i] array to track maximum profit for transactions before i (including i), and right[i + 1] to track maximum profit for transcations after i.

  1. Prices: 1 4 5 7 6 3 2 9
  2. left = [0, 3, 4, 6, 6, 6, 6, 8]
  3. right= [8, 7, 7, 7, 7, 7, 7, 0]

Time complexity O(n), space cost O(n).

  1. public class Solution {
  2. public int maxProfit(int[] prices) {
  3. if (prices == null || prices.length < 2)
  4. return 0;
  5. int length = prices.length, min = prices[0], max = prices[length - 1], tmpProfit = 0;
  6. int[] leftProfits = new int[length];
  7. leftProfits[0] = 0;
  8. int[] rightProfits = new int[length];
  9. rightProfits[length - 1] = 0;
  10. // Calculat left side profits
  11. for (int i = 1; i < length; i++) {
  12. if (prices[i] > min)
  13. tmpProfit = Math.max(tmpProfit, prices[i] - min);
  14. else
  15. min = prices[i];
  16. leftProfits[i] = tmpProfit;
  17. }
  18. // Calculate right side profits
  19. tmpProfit = 0;
  20. for (int j = length - 2; j >= 0; j--) {
  21. if (prices[j] < max)
  22. tmpProfit = Math.max(tmpProfit, max - prices[j]);
  23. else
  24. max = prices[j];
  25. rightProfits[j] = tmpProfit;
  26. }
  27. // Sum up
  28. int result = Integer.MIN_VALUE;
  29. for (int i = 0; i < length - 1; i++)
  30. result = Math.max(result, leftProfits[i] + rightProfits[i + 1]);
  31. result = Math.max(result, leftProfits[length - 1]);
  32. return result;
  33. }
  34. }

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. ios 计算文字的尺寸

    /** * 计算文字尺寸 * @param text 需要计算尺寸的文字 * @param font 文字的字体 * @param maxSize 文字的最大尺寸 */ - (CGSize)sizeW ...

  2. Filter简单介绍

    一.简单介绍 Filter也称为过滤器,WEB开发者通过Filter技术.对webserver管理的全部web资源:比如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截.从而实 ...

  3. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  4. DE2带的IP核ISP12362报错问题解决 Error:avalon_slave_1_irq: associatedAddressablePoint out of range

    问题来源与对友晶提供的ISP1362 IP核的使用,由于Quartus II版本问题,它提供的IP基于7.0版本,而我用的版本为11.1,在SOPC Builder中重新加载IP,就出现了上述的错误报 ...

  5. 前端--关于javascript函数

    终于可以说说函数了,函数是javascript设计最出色的地方,可以说它是所有概念中最重要的一个,因为围绕函数而阐述的周边概念涵盖了javascript的方方面面,所以理解了函数可以说对javascr ...

  6. jquery简单切换插件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  7. CSS筛选器简单实例1

    1.通配符 <!--筛选器---通配符实例--> <!--支持IE7+ --> <style type="text/css"> *.all { ...

  8. Android SQLite的使用2(非原创)

    1.数据库的增.删.改.查:execSQL方法 public void insertAction() {//添加信息 db.execSQL("insert into Emp(name,sal ...

  9. Windows I/O模型之一:Select模型

    1.概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock) 四种调用模式: 同步:所谓同步,就是在发出一个功能调用时,在没有得到结果 ...

  10. (原)Microsoft Source Reader的简单使用

    感觉Microsoft Source Reader还是比较坑的,只是由于需要,不得不使用.其实按照Microsoft提供的示例,基本上可以正常的调试出程序来. 下面的例子,简单的给出了Source R ...