【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 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) { int n=prices.size();
if(n==)return ; vector<int> dpBack(n),dpAfter(n); int maxProfit=;
dpBack[]=;
int left=prices[]; for(int i=;i<n;i++)
{
if(prices[i]>left)
{
if(maxProfit<prices[i]-left) maxProfit=prices[i]-left;
}
else
{
left=prices[i];
} dpBack[i]=maxProfit;
} maxProfit=;
dpAfter[n-]=;
int right=prices[n-]; for(int j=n-;j>=;j--)
{
if(prices[j]<right)
{
if(maxProfit<right-prices[j]) maxProfit=right-prices[j];
}
else
{
right=prices[j];
} dpAfter[j]=maxProfit;
} int result=;
for(int i=;i<n-;i++)
{
if(dpBack[i]+dpAfter[i+]>result) result=dpBack[i]+dpAfter[i+]; if(result<dpBack[i+]) result=dpBack[i+];
} return result;
}
};
【leetcode】Best Time to Buy and Sell Stock III的更多相关文章
- 【LeetCode】Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- 【leetcode】Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- 【leetcode】121-Best Time to Buy and Sell Stock
problem 121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<i ...
- 【LeetCode】Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [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 ...
- 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 ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock III
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...
- 【数组】Best Time to Buy and Sell Stock I/II
Best Time to Buy and Sell Stock I 题目: Say you have an array for which the ith element is the price o ...
随机推荐
- Java常见异常
1. runtimeException子类: 1. java.lang.ArrayIndexOutOfBoundsException 数组索引越界异常.当对数组的索引值为负数或大于等于数组大小时 ...
- Java基础-String、StringBuffer、StringBuilder
看下面这段代码: public class Main { public static void main(String[] args) { String string = ""; ...
- 缓存插件 EHCache 对象缓存(Spring)
对象缓存就是将查询的数据,添加到缓存中,下次再次查询的时候直接从缓存中获取,而不去数据库中查询. 对象缓存一般是针对方法.类而来的,结合Spring的Aop对象.方法缓存就很简单.这里需要用到切面编程 ...
- 【CodeForces 624D】Array GCD
题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...
- 【bzoj1562】 NOI2009—变换序列
http://www.lydsy.com/JudgeOnline/problem.php?id=1562 (题目链接) 题意 给出一个序列(0~n-1),这个序列经过某个变换会成为另外一个序列,但是其 ...
- Server Data Synchronization Via Linux rsync、rsync+inotify Between Load Balance Server
目录 . 远程文件同步的应用场景 . rsync+crontab . rsync+inotify 1. 远程文件同步的应用场景 在负载均衡集群的应用场景中,往往在多台web server的前端有一个提 ...
- POJ1753Flip Game(DFS + 枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37050 Accepted: 16122 Descr ...
- 依赖管理工具漫谈--从Maven,Gradle到Go
http://jolestar.com/dependency-management-tools-maven-gradle/
- hdu 2018 母牛的故事
#include<stdio.h> int main(void) { int i,n,j,k; long long narr[60]; narr[1]=1; narr[2]=2; narr ...
- Java I/O操作
按字节读取读取文件,并且将文件里面的内容写到另外一个文件里面去 public class CopyBytes { public static void main(String[] args) thro ...