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. Hibernate中的inverse和cascade属性

    Hibernate中的inverse和cascade属性 inverse的值有两种,"true"和"false".inverse="false&quo ...

  2. 秒杀Sublime Text的微软开源代码编辑工具Visual Studio Code

    1. 下载链接: https://code.visualstudio.com/ 2. 秒开一个ASP.NET网站源码 3.编辑CSS颜色支持 4.Git支持 5.常用快捷键 Ctrl+Shift+P ...

  3. pickle 两个使用小方法

    def pickle_load(file_path): f = open(file_path,'r+') data = pickle.load(f) f.close() return data     ...

  4. Python 中print 和return 的区别

    1.print() print()函数的作用是输出数据到控制台,就是打印在你能看到的界面上. 2.return return语句[表达式]退出函数,选择性地向调用方返回一个表达式.不带参数值的retu ...

  5. Python3基础教程(十八)—— 测试

    编写测试检验应用程序所有不同的功能.每一个测试集中在一个关注点上验证结果是不是期望的.定期执行测试确保应用程序按预期的工作.当测试覆盖很大的时候,通过运行测试你就有自信确保修改点和新增点不会影响应用程 ...

  6. react中的jsx详细理解

    这是官网上的一个简单的例子 const name = 'Josh Perez'; const element = <h1>Hello, {name}</h1>; ReactDO ...

  7. JavaSE-02 变量 数据类型和运算符

    学习要点 掌握变量的概念 掌握常用数据类型 掌握赋值运算符.算术运算符 掌握boolean数据类型和关系运算符 掌握变量的概念 面向过程程序的定义 程序的定义:程序=数据+算法+文档 程序要操作的数据 ...

  8. 错误的语法:"create view必须是批处理中仅有的语句"

    编写脚本提示: 错误的语法:"create view必须是批处理中仅有的语句" FROM sys.views WHERE name = 'v_CS_UserRoleNames' ) ...

  9. CodeForces - 930A Peculiar apple-tree(dfs搜索)

    题目: 给出一个树,这棵树上每个结点每一秒都会结出一颗果实,果实每经过一秒就会落向下一个结点,如果一个结点在同一时刻上的果实两两抵消,问最后在根节点处一共有多少个果实. 思路: dfs直接搜索统计这棵 ...

  10. MySQL数据库常见面试题

    什么是存储过程?有哪些优缺点? 存储过程简单来说就是为了以后使用而保存的一条或多条预编译SQL语句,这些语句块像一个方法一样执行一些功能. 优点: 类似于封装,简化操作: 不用反复建立一系列处理步骤, ...