最多两次股票交易-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).
class Solution {
public:int maxProfit(vector<int> &prices) {if(prices.size()==0)return 0;int n=prices.size();vector<int> left(n);vector<int> right(n);int min=prices[0];int max=prices[n-1];int res=0;for(int i=1;i<n;i++){min=min<prices[i]?min:prices[i];left[i]=left[i-1]>(prices[i]-min)?left[i-1]:(prices[i]-min);}for(int j=n-2;j>=0;j--){max=max>prices[j]?max:prices[j];right[j]=right[j+1]>(max-prices[j])?right[j+1]:(max-prices[j]);}for(int i=0;i<n;i++){res=res>(left[i]+right[i])?res:(left[i]+right[i]);}return res;}
};
转载于:https://www.cnblogs.com/Vae1990Silence/p/4830586.html
最多两次股票交易-Best Time to Buy and Sell Stock III相关推荐
- 【DP + 卖股票】LeetCode 123. Best Time to Buy and Sell Stock III
LeetCode 123. Best Time to Buy and Sell Stock III Solution1: 不得不让人感叹算法之精妙啊!!! 参考网址:[1]http://www.cnb ...
- Leetcode Best Time to Buy and Sell Stock III
Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...
- Best Time to Buy and Sell Stock III
解题思路来自:https://blog.csdn.net/u012501459/article/details/46514309 Say you have an array for which the ...
- Leetcode_123_Best Time to Buy and Sell Stock III
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43740415 Say you have an array ...
- [LeetCOde][Java] 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 ...
- Best Time to Buy and Sell Stock III O(n) 求解方法
leetcode的题目:http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ leetcode的题目都很简练,但是很 ...
- 【Best Time to Buy and Sell Stock III 】cpp
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- [LeetCode 123] - 买入与卖出股票的最佳时机III(Best Time to Buy and Sell Stock III)
问题 假设你有一个数组,其中的第i个元素表示一只股票在第i天的价格. 设计一个算法找出最大的利润值.你最多只能进行两次交易. 注意: 你不能在同一时间进行多次交易(即你必须在再次买入股票之前卖出当前的 ...
- 123 Best time to buy and sell stock iii
题解: 根据题目要求,最多进行两次买卖股票,而且手中不能有2只股票,就是不能连续两次买入操作. 所以,两次交易必须是分布在2各区间内,也就是动作为:买入卖出,买入卖出. 进而,我们可以划分为2个区间[ ...
最新文章
- 打印容器_喷墨打印MnO?制备微型超级电容器
- jedis中scan的实现
- 设计模式之SOLID原则再回首
- 【计组实验】P2 Modelsim Verilog单周期处理器开发 MIPS指令集
- 使用Activator.CreateInstance完善简单工厂
- javascript 手机手势动作touch触屏原理分析
- Windows Phone 7 使用Canvas Grid StackPanel进行布局管理
- 表达式必须是常量表达式
- ubuntu阿里云盘(小白羊)
- 123457123457#0#-----com.yuming.ZuiNiuChengYu--前拼后广--最牛成语
- 开源 java CMS - FreeCMS2.8 移动app站点配置
- ISP——AWB(Auto White Balance)
- 编码,隐匿在计算机软硬件背后的语言读书笔记(作者序)
- vue 视频截图获取第一帧或者某一帧
- 【强化学习】分层强化学习
- 自动化测试和软件测试的区别,自动化测试和手动测试之间的区别
- 电脑下载软件用什么软件好?安卓手机下载软件用哪个软件好?IDM下载器说:在做的都是弟弟
- HTML中字体属性、文本属性使用说明
- 友盟统计-事件跟踪的部署
- 根据卡号查询所属银行
热门文章
- sublime text使用正则表达式批量给KV加
- 【视频】vue组件之$emit父子组件间值的传递
- 实现ftp_FTP文件服务器的实现
- 如何快速学会嵌入式?
- void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)的一些理解
- 我的世界修改服务器头像,我的世界单机模式更换头像的方法 各种怪物的头等来换...
- react 组件怎么公用_React、Redux与复杂业务组件的复用
- golang 数组 最后一个_面试必问:Golang高阶Golang协程实现原理
- python视频处理教程_Python视频编辑库:MoviePy|python爬虫|python入门|python教程
- JSON / 格式详解