Description:

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

给出股价表,最多两次交易,求出最大收益。

动态规划,分解成两个子问题,在第i天之前(包括第i天)的最大收益,在第i天之后(包括第i天)的最大收益,这样就变成了求一次交易的最大收益了,同系列问题I,

最后遍历一次求子问题最大收益之和的最大值,就是最后的解。时间复杂度O(n),空间复杂度O(n)

public class Solution {
public int maxProfit(int[] prices) {
int len = prices.length; if(len == 0) return 0; int[] leftProfit = new int[len];
int[] rightProfit = new int[len]; Arrays.fill(leftProfit, 0);
Arrays.fill(rightProfit, 0); //计算左边的最大收益,从前向后找
int lowestPrice = prices[0];
for(int i=1; i<len; i++) {
lowestPrice = min(lowestPrice, prices[i]);
leftProfit[i] = max(leftProfit[i-1], prices[i] - lowestPrice);
} //计算右边的最大收益,从后往前找
int highestPrice = prices[len-1];
for(int i=len-2; i>=0; i--) {
highestPrice = max(highestPrice, prices[i]);
rightProfit[i] = max(rightProfit[i+1], highestPrice-prices[i]);
} int maxProfit = leftProfit[0] + rightProfit[0];
//遍历找出经过最多两次交易后的最大了收益
for(int i=1; i<len; i++) {
maxProfit = max(maxProfit, leftProfit[i] + rightProfit[i]);
} return maxProfit;
} public int min(int a, int b) {
return a > b ? b : a;
} public int max(int a, int b) {
return a > b ? a : b;
}
}

LeetCode——Best Time to Buy and Sell Stock III的更多相关文章

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

  2. [LeetCode] 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 ...

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

    将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...

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

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

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

  6. [leetcode]Best Time to Buy and Sell Stock III @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...

  7. leetcode -- Best Time to Buy and Sell Stock III TODO

    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——Best Time to Buy and Sell Stock III (股票买卖时机问题3)

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

  9. LeetCode OJ--Best Time to Buy and Sell Stock III

    http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 这三道题,很好的进阶.1题简单处理,2题使用贪心,3题使用动态 ...

随机推荐

  1. 数据库——SQL中EXISTS怎么用2(转)

    数据库sql语句的exists总结 sql exists in 学习 先来比较下语法: --deals=交易表,areas=地域表,例如香港:我们的目的:查看有交易的地域 select * from ...

  2. 飞飞影视cms标签

    解析范围: 全站所有模板均可调用 模板名称: 所有模板 调用函数: ff_mysql_vod(参数1:值1;参数2:值2;参数3,值3....;参数n,值n),支持的参数列表如下 * 以下变量名如$v ...

  3. hdu 1595 find the longest of the shortest(dijstra + 枚举)

    http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...

  4. WHAT EXACTLY IS WASM ?!

    终于, 我入门了当初很仇视的技术.... 什么是WebAssembly? WebAssembly或WASM是一个编译器目标(由编译器生成的代码),具有二进制格式,允许我们在浏览器上执行C,C ++和R ...

  5. Ajax-ajax实例1-动态加载的 FAQ

    动态加载 FAQ 的过程主要是利用 XMLHttpRequest(以下简称 XHR)对象与服务端通信,根据用户单击的感兴趣问题动态将内容加载到页面中.在具体实现时,有两点要注意的内容. 1 .对每个问 ...

  6. SQL SERVER 2005快捷键

    一.SQL SERVER 2005快捷键 快捷键 功能 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 ...

  7. C# 窗体显示避免抢夺焦点

    通过调用API进行显示可以避免抢夺焦点的问题 以下是API调用 using System.Runtime.InteropServices; [DllImport("user32.dll&qu ...

  8. spring只是一个框架

    想跟着 spring in action 4 系统的研究下spring,结果发现忘了怎么建一个spring项目. 关键是,不知道该建一个什么项目,Java项目?Maven项目(Java项目?Web项目 ...

  9. tiny210V2开发板hdmi输出到10.1寸LCD,无图像

    tiny210V2开发板hdmi输出到10.1寸LCD,无图像... 用tiny210V2开发板的HDMI接口输出到的10.1寸LCD,LCD无任何现象.说明一下我的情况,我的10.1寸屏LCD是HD ...

  10. vs2013+MVC3.0+EasyUI的ComboBox联动使用(二)

     vs2013+MVC3.0+EasyUI的ComboBox联动使用(二) 简单介绍:在vs2013(.net4.0)中使用MVC3.0对于EasyUI中ComboBox的联动使用. 载入Comb ...