Best Time to Buy and Sell Stock I II III
int maxProfit(vector<int> &prices) {
if(prices.size() ==0)
return 0;
int min=prices[0], profit=0;
for(int i=0; i<prices.size(); i++)
{
if(prices[i] - min > profit)
profit = prices[i] - min;
if(prices[i] < min)
min = prices[i];
}
return profit;
}
II
int maxProfit(vector<int> &prices) {
if (prices.size() == 0)
return 0;
int profit = 0, tmpMin=prices[0], tmpMax=prices[0];
int n = prices.size();
for (int i = 1; i < n; i++)
{
//寻找比tmpMIN小的元素
if (prices[i] < tmpMin)
{
//比tmpMin小,则更新较小的
tmpMin = prices[i];
}
else
{
//比tmpMin大,则可以出手卖掉,并更新tmpMin
profit += (prices[i]-tmpMin);
tmpMin = prices[i];
}
}
return profit;
}
III
//取[m, n]之间的最大利润
int Profit1(vector<int>& prices, int m, int n)
{
int size = prices.size();
if(m >= size)
return 0;
int minPrice = prices[m], profit = 0;
for (int i = m+1; i < n; i++)
{
if (prices[i] - minPrice > profit)
{
profit = prices[i] - minPrice;
}
if (prices[i] < minPrice)
{
minPrice = prices[i];
}
}
return profit;
}
int maxProfit(vector<int> &prices)
{
if(prices.size() == 0)
return 0;
int n = prices.size();
int max1 = 0;
for (int i = 1; i < n; i++)
{
int tmp1 = Profit1(prices, 0, i);
int tmp2 = Profit1(prices, i+1, n);
if(tmp1 + tmp2 > max1)
max1 = tmp1 + tmp2;
}
return max1;
}
方法二,DP,参考了一位网友的方法: http://blog.youkuaiyun.com/pickless/article/details/12034365
int maxProfit(vector<int> &prices)
{
int n = prices.size();
if(0==n || 1==n)
return 0;
int * l = new int[n];
int * r = new int[n];
memset(l, 0, sizeof(int) * n);
memset(r, 0, sizeof(int) * n);
int minPrice = prices[0];
for (int i = 1; i < n; i++)
{
l[i] = prices[i] - minPrice > l[i-1]? prices[i] - minPrice : l[i-1];
minPrice = prices[i] < minPrice ? prices[i] : minPrice;
}
int maxPrice = prices[n-1];
for (int i = n-2; i >=0; i--)
{
r[i] = maxPrice - prices[i] > r[i+1] ? maxPrice - prices[i] : r[i+1];
maxPrice = prices[i] > maxPrice ? prices[i] : maxPrice;
}
int profit = 0;
for (int i = 0; i < n; i++)
{
profit = l[i] + r[i] > profit ? l[i] + r[i] : profit;
}
delete l;
delete r;
return profit;
}
690



被折叠的 条评论
为什么被折叠?



