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. 总结一些js自定义的函数

    1.dayin() 作用:将id为dayin的内容,新建页面并打印,可解决打印某页面中的部分内容的问题. 使用方法:将要打印的内容通过 <span id="dayin"> ...

  2. 关于对象映射(Dto->model) 思路的一些想法

    最近粗浅的学习了下AutoMapper 这个做对象映射的第三方工具,觉得非常方便使用,所以简单的总结了一下我能想到的简单的对象映射的方式. 占时先不考虑源对象成员到目标对象成员的指定映射(即成员名不一 ...

  3. Lintcode---克隆二叉树

    深度复制一个二叉树. 给定一个二叉树,返回一个他的 克隆品 . 您在真实的面试中是否遇到过这个题? Yes 样例 给定一个二叉树: 1 / \ 2 3 / \ 4 5 返回其相同结构相同数值的克隆二叉 ...

  4. struts2 的国际化

    一.使用步骤 1)写资源文件,资源文件名命名规范和之前的讲的一致,有疑问请参考java开发中国际化 2)配置,在 struts.xml 中使用常量进行加载 struts.custom.i18n.res ...

  5. Node.js + Express + Ubuntu

    1 . 怎么在ubuntu中,background的方式 启动express. 网站www /home/host/express/web/bin# nohup node www 2. Node.js的 ...

  6. [svc]nfs客户端报错解决Stale file handle

    NFS故障: 问题背景: 客户端挂载是好的.服务端磁盘满了,重新给挂了一快.客户端df -h 发现nfs挂载消失. 查看目录客户端报错:Stale file handle 现象如下: [root@n1 ...

  7. [POJ 1236][IOI 1996]Network of Schools

    Description A number of schools are connected to a computer network. Agreements have been developed ...

  8. android.view.animation(3) - LayoutAnimationController 和 GridLayoutAnimationController

    前几篇给大家讲述了如何针对某一个控件应用动画,这篇将给大家讲解如何给容器中的控件应用统一动画.即在容器中控件出现时,不必为每个控件添加进入动画,可以在容器中为其添加统一的进入和退出动画. 从上面的示例 ...

  9. win32环境下显示中文

    //编码转换 //#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) // string title = "成绩"; // GBK2UTF8 ...

  10. Ubuntu12.04 修复GRUB

    电脑安装了双系统,本来好好的GRUB管理启动,在重装过之后就只能进win7了,所以尝试将GRuB重新安装到mbr,使用GRUB作为启动管理程序. 1.制作U盘系统 使用软碟通,讲Ubuntu12.04 ...