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

 public class Solution {
public int maxProfit(int[] prices) {
// Start typing your Java solution below
// DO NOT write main() function
if(prices.length == 0){
return 0;
}
int result = Integer.MIN_VALUE;
int[] profits = new int[prices.length];
int min = Integer.MAX_VALUE, maxBeforeI = Integer.MIN_VALUE;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < min) {
min = prices[i];
}
if (prices[i] - min > maxBeforeI) {
maxBeforeI = prices[i] - min;
}
profits[i] = maxBeforeI;
}
int max = Integer.MIN_VALUE;
for (int i = prices.length - 1; i >= 0; i--) {
if (prices[i] > max) {
max = prices[i];
} int profit = max - prices[i]; if (profit + profits[i] > result) {
result = profit + profits[i];
}
}
return result;
}
}

m transactions solution not understand

http://discuss.leetcode.com/questions/287/best-time-to-buy-and-sell-stock-iii

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

  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

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

  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. ubuntu截图工具

    ubuntu截图工具   首先,我们用apt-get  install 去安装一个,scrot 主要用在命令行下,它使用 imlib2 库来抓取并保存图像                 sudo a ...

  2. 使用Ubuntu12.04创建无线WiFi热点供手机上网

    [日期:2012-10-10]   1,单击右上角网络连接管理器(记得打开电脑的无线网络开关),选择“编辑连接…”   2,选择无线,然后单击添加.   3,{无线}输入连接名称,如longer,然后 ...

  3. jquery 属性操作 attr( ) prop()css( )区别

    一 attr () 和 prop( ) 操作属性 谈谈我的总结: 1 2 1 属性的定义,根据W3C手册所述:属性包括,标准属性:id class style title 语言属性 lang dir以 ...

  4. sql server数据库查询超时报错

    报错信息如下: 链接服务器"DBJointFrame"的 OLE DB 访问接口 "SQLNCLI10" 返回了消息 "查询超时已过期". ...

  5. chrome 浏览器插件开发

    一.chrome 浏览器插件开发是什么: 1 从技术上说插件只是一个存在于本地的一个网站.所以呢在插件开发的过程中用到的技术无非是 javascript .html .css . 二.把当前活动页面的 ...

  6. MySQL-Transfer2.3发布

    Transfer 2.3发布,下载地址 此版本除了升级based版本外 *优化了无索引表的同步性能 *优化了slave模式下超大事务内存消耗问题 *Transfer模式相关的功能改动较多 *修复tra ...

  7. jQuery事件:bind、delegate、on的区别

    最近在AngularJS的开发中,遇到一个神奇的事情:我们用到livebox来预览评论列表中的图片, 然而评论列表是由Angular Resource动态载入的.不可思议的是,点击这些动态载入的图片仍 ...

  8. 飘逸的python - ord和chr以及unichr

    ord是unicode ordinal的缩写,即编号 chr是character的缩写,即字符 ord和chr是互相相应转换的. 可是因为chr局限于ascii,长度仅仅有256. 于是又多了个uni ...

  9. Atitit. 解决unterminated string literal 缺失引号

    Atitit. 解决unterminated string literal 缺失引号 原因:::或许string没使用引号括号起来...missingMessage缺失了一个单个的引号 Error:  ...

  10. SAP ERP 6.0 EHP7 SR2(WINDOWS MSSQL版)安装说明

    原文 by 枫竹丹青 ⋅ 1.安装准备 1.1.版本说明 本文是描述在一个Windows虚拟机.SQL Server数据库环境下,安装SAP ERP 6.0 EHP7 SR2服务器,安装完成虚拟机文件 ...