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. centos下node.js的安装

    安装的路径我举例在home目录 1.cd /home 2.下载node.js最新版本 wget http://nodejs.org/dist/v0.10.28/node-v0.10.28.tar.gz ...

  2. Android软件开发之EditText 详解

    EditText在API中的结构 java.lang.Objectandroid.view.Viewandroid.widget.TextView        android.widget.Edit ...

  3. docker 容器自启动

    我们设置了docker自启动后,docker可以管理各种容器了,对于容器我们也可以设置重启的策略. 在容器退出或断电开机后,docker可以通过在容器创建时的--restart参数来指定重启策略: # ...

  4. DevExpress实现根据行,列索引来获取RepositoryItem的方法

    /// <summary> /// 根据行,列索引来获取RepositoryItem /// </summary> /// <param name="view& ...

  5. 蜜果私塾:informix数据库学习合集[不断补充]

    一.infomix使用备忘录     目录结构:     1. 启动与停止命令:      2. 修改数据库编码:      3. 查看informix占用的端口:      4. 使用dbacces ...

  6. Android系统应用信息中存储和缓存的计算方法

    进行例如以下操作: 设置->应用->选择一个应用->应用信息 会到达例如以下界面: 能够看到这个应用占用的磁盘空间. 先说结果,这几项会计算哪些文件(夹). 1.应用,由三项相加组成 ...

  7. Entity Framework底层操作封装V2版本号(2)

    这个类是真正的数据库操作类.上面的那个类仅仅是调用了这个封装类的方法进行的操作 using System; using System.Collections.Generic; using System ...

  8. 禁止用户登陆的 /bin/false和/sbin/nologin的区别

    1 区别 /bin/false是最严格的禁止login选项,一切服务都不能用. /sbin/nologin只是不允许login系统  小技巧: 查看 /etc/passwd文件,能看到各用户使用的sh ...

  9. 响应式布局框架 Pure-CSS 5.0 示例中文版-下

    10. 表格 Tables 在 table 标签增加 .pure-table 类 <table class="pure-table"> <thead> &l ...

  10. MySQL是如何做到安全登陆

    首先Mysql的密码权限存储在mysql.user表中.我们不关注鉴权的部分,我们只关心身份认证,识别身份,后面的权限控制是很简单的事情.在mysql.user表中有个authentication_s ...