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

这道题要求我们最多做两笔交易,求其最大利润。此题是DP题目,需要写出状态方程,即profit = transaction(1)+transaction(2)=sell(1)-buy(1)+sell(2)-buy(2)。代码如下:

public class Solution {

public int maxProfit(int[] prices) {

int buy1 = Integer.MIN_VALUE;

int buy2 = Integer.MIN_VALUE;

int sell1 = 0;

int sell2 = 0;

for(int i=0;i<prices.length;i++){

buy1 = Math.max(buy1,-prices[i]);

sell1 = Math.max(sell1,prices[i]+buy1);

buy2 = Math.max(buy2,sell1-prices[i]);

sell2 = Math.max(sell2,buy2+prices[i]);

}

return sell2;

}

}

123. Best Time to Buy and Sell Stock III ~~的更多相关文章

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

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

  3. 【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 ...

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

  5. 【刷题-LeetCode】123 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 ...

  6. 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 a ...

  7. LeetCode OJ 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 ...

  8. 123. Best Time to Buy and Sell Stock III ——LeetCode

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

  9. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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

  10. 123. Best Time to Buy and Sell Stock III (Array; DP)

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

随机推荐

  1. 开发原生安卓cordova插件(有原生界面)

    上文开发的插件没有调用原生界面,本文介绍开发带有activity的插件 本文很多操作与上文重复,重复部分会省略 首先打开plug1,先开发插件的原生代码 在以下命名空间创建一个activity 名称为 ...

  2. 严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:解决

    严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal Contain ...

  3. SqlSessionFactory

    源码: public interface SqlSessionFactory { SqlSession openSession(); SqlSession openSession(boolean va ...

  4. bat判断服务是否启动

    sc query|find "tomcat6" && echo yes || echo nosc query|find "eventlog" & ...

  5. win驱动安装记录

    工具:devcon64.exe 安装/更新/删除等记录:c:\windows\inf\setupapi.dev.log

  6. 洛谷 P1011 车站

    题目描述 火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人.从第3站起( ...

  7. learnpythonthehardway EX41 相关

    str.count() # str.count()方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. # str.count(sub, start= 0,end=len( ...

  8. html5shiv.js让吃屎的IE6、IE7、IE8支持html5去吧

    插件介绍 用于解决IE9以下版本浏览器对HTML5新增标签不识别,并导致CSS不起作用的问题.所以我们在使用过程中,想要让低版本的浏览器,即IE9以下的浏览器支持,那么这款html5shiv.js是一 ...

  9. 如何在windows 2008 IIS7 上实现AD域的访问控制

    1.服务器加入域 2.创建点站 3.对站站进行设置 3.1设置网站的连接模式 选中站点,在控制台右侧 选择 基本设置 => 选择 应用程序用户 3.2 开启访问模式 选择站点 => 身份验 ...

  10. HTTP初步了解

    HTTP协议简介 HTTP(超文本传输协议)是应用层上的一种客户端/服务端模型的通信协议,它由请求和响应构成,且是无状态的.(暂不介绍HTTP2) 协议 协议规定了通信双方必须遵循的数据传输格式,这样 ...