LeetCode:Best Time to Buy and Sell Stock III
来源:互联网 发布:数码印刷软件 编辑:程序博客网 时间:2024/05/01 12:19
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).
分析:这一题约束最多只能买卖两次股票,并且手上最多也只能持有一支股票。因为不能连续买入两次股票,所以买卖两次肯定分布在前后两个不同的区间。设p(i) = 区间[0,1,2...i]的最大利润 + 区间[i,i+1,....n-1]的最大利润(式子中两个区间内分别只能有一次买卖,这就是第一道题的问题),那么本题的最大利润 = max{p[0],p[1],p[2],...,p[n-1]}。根据第一题的算法2,我们可以求区间[0,1,2...i]的最大利润;同理可以从后往前扫描数组求区间[i,i+1,....n-1]的最大利润,其递归式如下:
dp[i-1] = max{dp[i], maxprices - prices[i-1]} ,maxprices是区间[i,i+1,...,n-1]内的最高价格。
因此两趟扫描数组就可以解决这个问题,代码如下:
class Solution {public: int maxProfit(vector<int> &prices) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. const int len = prices.size(); if(len <= 1)return 0; int maxFromHead[len]; maxFromHead[0] = 0; int minprice = prices[0], maxprofit = 0; for(int i = 1; i < len; i++) { minprice = min(prices[i-1], minprice); if(maxprofit < prices[i] - minprice) maxprofit = prices[i] - minprice; maxFromHead[i] = maxprofit; } int maxprice = prices[len - 1]; int res = maxFromHead[len-1]; maxprofit = 0; for(int i = len-2; i >=0; i--) { maxprice = max(maxprice, prices[i+1]); if(maxprofit < maxprice - prices[i]) maxprofit = maxprice - prices[i]; if(res < maxFromHead[i] + maxprofit) res = maxFromHead[i] + maxprofit; } return res; }};
转自:http://www.cnblogs.com/TenosDoIt/p/3436457.html
0 0
- LeetCode Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock III
- leetcode 16: Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III
- [LeetCode] Best Time to Buy and Sell Stock III
- leetcode 62: Best Time to Buy and Sell Stock III
- [LeetCode] Best Time to Buy and Sell Stock III
- 【leetcode】Best Time to Buy and Sell Stock III
- LeetCode - Best Time to Buy and Sell Stock III
- LeetCode之Best Time to Buy and Sell Stock III
- [LeetCode]Best Time to Buy and Sell Stock III
- LeetCode: Best Time to Buy and Sell Stock III
- [Leetcode]Best Time to Buy and Sell Stock III
- [leetcode]Best Time to Buy and Sell Stock III
- leetcode题目:Best Time to Buy and Sell Stock III
- [LeetCode] Best Time to Buy and Sell Stock III
- LeetCode-Best Time to Buy and Sell Stock III
- MongoDB使用初步
- IE有时候调试代码时有界面弹不出来
- Oracle11gR2——关于ASM
- Android-----mediastore
- 黑马程序员——OC语言中的类和对象
- LeetCode:Best Time to Buy and Sell Stock III
- 利用SDK与MFC实现画线功能的五种方法
- java如何配置JDK环境变量
- ARM汇编指令(B/BL/BX)
- AndroidStudio快捷方式总结(官方文档版)
- 常用meta整理
- SQL Server 日期和时间函数
- 设置SVN忽略文件和目录(文件夹)
- 声明一个栈接口