maxProfit

#include
#include
#include
using namespace std;
int maxProfit(vector&prices)
{
const int n=prices.size();
if(n<2)
return 0;
vectorf(n,0);
vectorg(n,0);
for(int i=1,valley=prices[0];i {
valley=min(valley,prices[i]);
f[i]=max(f[i],prices[i]-valley);
}
for(int i=n-2,peak=prices[n-1];i>=0;--i)
{
peak=max(peak,prices[i]);
g[i]=max(g[i],peak-prices[i]);
}
int ret=0;
for(int i=0;i ret=max(ret,f[i]+g[i]);
return ret;
}
int main()
{
int a[]={-2,4,3,1,-5,3,7,-6,3};
vectorv(a,a+sizeof(a)/sizeof(int));
cout< return 0;
}

你可能感兴趣的:(C++)