111_leetcode_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).
1:特殊情况。2:从前到后和从后到前遍历两次数字;3:在遍历的过程中,保存以当前索引为结束点或者開始点的仅仅进行一次买卖的最大收益。4:注意边界结束条件
int maxProfit(vector<int> &prices)
{
if(prices.size() <= 1)
{
return 0;
} int size = (int)prices.size();
vector<int> leftProfit(size, 0);
leftProfit[0] = 0;
int minValue = prices[0]; int result = 0; for(int i = 1; i < size; i++)
{
if(prices[i] > minValue)
{
leftProfit[i] = (prices[i] - minValue > leftProfit[i-1] ? prices[i] - minValue : leftProfit[i-1]);
}
else
{
minValue = prices[i];
leftProfit[i] = leftProfit[i-1];
}
} result = leftProfit[size-1] > leftProfit[size-2] ? leftProfit[size-2] : leftProfit[size-1];
int maxValue = prices[size -1];
int rightMaxProfit = 0; for(int i = size - 2; i >= 0; i--)
{
if(prices[i] < maxValue)
{
rightMaxProfit = (rightMaxProfit > maxValue - prices[i] ? rightMaxProfit : maxValue - prices[i]);
}
else
{
maxValue = prices[i];
} if(i == 0)
{
result = (result > rightMaxProfit ? result : rightMaxProfit);
}
else
{
result = (result > rightMaxProfit + leftProfit[i-1] ? result : rightMaxProfit + leftProfit[i-1]);
}
} return result;
}
111_leetcode_Best Time to Buy and Sell Stock III的更多相关文章
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- LeetCode 笔记23 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 ...
- Best Time to Buy and Sell Stock | & || & III
Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...
- 【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 ...
- 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 ...
- 【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: Best Time to Buy and Sell Stock III 解题报告
Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...
- [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 ...
- 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 ...
随机推荐
- crtmpserver系列之一:流媒体概述
阅读目录 概述 流媒体系统的组成 媒体文件封装 传输协议 回到顶部 概述 所谓流媒体按照字面意思理解就是像流一样的媒体,看起来像是废话.流媒体现在司空见惯,所以一般人大概不会有疑问.事实上在流媒体还没 ...
- JavaScript功能一览
// 10) throw "太大"; if(x0) { c_start=document.cookie.indexOf(c_name + "=") if (c_ ...
- dom操作之开关灯
<!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/h ...
- 利用if else 来计算车费
static void Main(string[] args) { while (true) { double ...
- Javascript 多线程?
最近在遇到一个问题:HTML页面中的页面无法刷新,只能在底层全部处理完成后才能进行页面刷新.在里面已经采用SetTimeout进行了处理,但是明显没有达到预期的效果. 主要的原因是对SetTimeou ...
- SharePoint Site Pages & Application Pages
转:http://www.wcode.net/plus/view.php?aid=1582071 SharePoint一个很重要的概念就是Site Pages和Application Pages.接触 ...
- JDK1.5新特性(四)……Autoboxing/Unboxing
援引 Autoboxing/Unboxing - This facility eliminates the drudgery of manual conversion between primitiv ...
- codeforce 651B Beautiful Paintings
题意:后一个比前一个大就加一,问最大次数. #include<cstdio> #include<cstring> #include<algorithm> #incl ...
- 输入A和B,计算并输出A+B
EOF是一个预定义的常量,等于-1. 输入A和B,计算并输出A+B Sample input: 1 5 10 20 Sample output: 6 30 #include <iostr ...
- Java 并发之共享对象
上一篇文章说的是,避免多个线程在同一时间访问对象中的同一数据,这篇文章来详细说说共享和发布对象. 在没有同步的情况下,我们无法预料编译器.处理器安排操作执行的顺序,经常会发生以为“一定会”发生的动作实 ...