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 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 Accepted
题目要求最多只能交易(买进卖出)两次。所以就可以用sell2[i]表示前i天第二次卖出后手中最多有的钱,buy2[i]表示前i天第二次买入后手中最多有的钱,sell1[i]表示前i天第一次卖出后手中最多有的钱,buy1[i]表示前i天第一次买入后手中最多有的钱,其中,假设一开始手中钱的数量为0。可以得到规律:
sell2[i] = max(sell2[i-1], buy2[i-1]+prices[i]);
buy2[i] = max(buy2[i-1], sell1[i-1]-prices[i]);
sell1[i] = max(sell1[i-1], buy1[i-1]+prices[i]);
buy1[i] = max(buy1[i-1], -prices[i]);
观察可以发现,上述四个变量的值只与前一状态有关,所以可以用单个的变量来代替数组。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int sell2 = 0, sell1 = 0, buy2 = numeric_limits<int>::min(), buy1 = numeric_limits<int>::min();
for (int i = 0; i < prices.size(); i++) {
sell2 = max(sell2, buy2+prices[i]);
buy2 = max(buy2, sell1-prices[i]);
sell1 = max(sell1, buy1+prices[i]);
buy1 = max(buy1, -prices[i]);
}
return sell2;
}
};
LN : leetcode 123 Best Time to Buy and Sell Stock III的更多相关文章
- [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 ...
- [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 ...
- Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 123. Best Time to Buy and Sell Stock III ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 123. Best Time to Buy and Sell Stock III (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Leetcode#123 Best Time to Buy and Sell Stock III
原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...
- 【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 ...
- 【刷题-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 ...
- 【leetcode】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 ...
随机推荐
- int&boolean——Java和C的一点小差别
Java和C的差别非常多.只是预计这一点非常多人都不知道. 今天面试时碰到这么道C语言题 求执行结果 int x = -1; while(!x!=0){ cout<<x<<en ...
- Thinkpad升级Window10无法安装expresscache
本人有一台Thinkpad T440s,自从看了这篇帖子12秒开机!ExpressCache SSD缓存加速,就给自己的小黑加持了一块固态硬盘.使用后效果确实很明显. 问题 自从系统自动升级到wind ...
- 2016/05/05 smarty ①分页 ② 查询后分页 ③缓存
samrty 分页 查询后分页 0505fch.php <?php include("init.inc.php"); include("DBDA.php&qu ...
- Koa2学习(七)使用cookie
Koa2学习(七)使用cookie Koa2 的 ctx 上下文对象直接提供了cookie的操作方法set和get ctx.cookies.set(name, value, [options])在上下 ...
- command 'gcc' failed with exit status 1
https://stackoverflow.com/questions/11094718/error-command-gcc-failed-with-exit-status-1-while-insta ...
- 编译FreePascal源代码(摘录自邮件询问)
为了尝试编译FreePascal,我搜了官方文档,并给几位作者都发了邮件询问,目前结果如下: http://wiki.lazarus.freepascal.org/Getting_Lazarus#Co ...
- 【Java报错】Message: 3 字节的 UTF-8 序列的字节 2 无效
报错logs 2015-03-10 10:15:32,360 ERROR [qtp32195030-27] [InvokeAfterValve.java:55] - javax.xml.stream. ...
- problem in Sourcetree
1.The date is commit date not the date of author 2.The log line is ordered by time, actually it sho ...
- UWP开发入门系列笔记之(零):UWP的前世今生
引言 在本篇文章中,可以掌握以下知识: 设备族群,如何决定目标设备 新的UI控件和新面板帮助你适应不同的设备特征 从Windows 8系统开始,微软就 引入了WindowsRT(Windows Run ...
- 深度学习 dns tunnel检测 使用统计特征 全连接网络——精度99.8%
代码如下: import numpy as np import tflearn from tflearn.layers.core import dropout from tflearn.layers. ...