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 ...
随机推荐
- sql server 笔记1--case、WAITFOR、TRY CATCH
一.case 转自:http://blog.csdn.net/add8849/article/details/576424 深入使用:http://blog.csdn.net/akuoma/artic ...
- 【iOS系列】-UITableViewCell的展开与收缩的实现思路
UITableViewCell的展开与收缩的实现思路 现在项目中很多地方都会用到,所以我这里介绍一种可以复用的思路,这与文章后面的Swift的实现思路不同,具体大家可自行对比. Demo项目地址 开始 ...
- redis缓存数据库的详解
1,什么是redis? Redis是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库 Redis与其他key-value缓存产品有以下三个特点: Redis支持数据的持久化,可以 ...
- HDU4185 Oil Skimming —— 最大匹配
题目链接:https://vjudge.net/problem/HDU-4185 Oil Skimming Time Limit: 2000/1000 MS (Java/Others) Memo ...
- HDU2389 Rain on your Parade —— 二分图最大匹配 HK算法
题目链接:https://vjudge.net/problem/HDU-2389 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others) ...
- BZOJ2761:不重复数字(splay效率对比)
给出N个数,要求把其中重复的去掉,只保留第一次出现的数. 例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 6 5 4. In ...
- (unix domain socket)使用udp发送>=128K的消息会报ENOBUFS的错误
一个困扰我两天的问题, Google和Baidu没有找到解决方法! 此文为记录这个问题,并给出原因和解决方法. 1.Unix domain socket简介 unix域协议并不是一个实际的协议族,而是 ...
- SimpliciTI协议栈
SimpliciTI组网过程介绍 1.SimpliciTI支持点对点和星形的网络拓扑结构. 下面介绍以AP为中心的SimpliciTI网路协议的星形拓扑结构通信过程 1)当ED节点上电之后就扫描信 ...
- 摘抄 - 不为人知的JS调用样式的方法---document.createElement().addRule(..)
很多人可能在调用css样式都是使用传统的方式调用其实有很多方法可以进行调用,如使用内嵌样式,在html直接加入样式,给定外部样式文件,在外部样式文件中使用 @import url(样式文件路径),这些 ...
- E20170519-ts
numeric adj. 数字的; 数值的; nibble vt. 啃,一点一点地咬(吃); rational adj. 理性的; 合理的; n. 合理的事物; [数] 有理数; numerato ...