标签:bsp logs size time toc -- res tor nbsp
// Best Time to Buy and Sell Stock 1
class Solution { public: int maxProfit(vector<int>& prices) { if(prices.size()==0) return 0; int maxres=prices[prices.size()-1]; int ans=0; for(int i=prices.size()-1;i>=0;i--) { maxres=max(maxres,prices[i]); ans=max(ans,maxres-prices[i]); } return ans; } };
// Best Time to Buy and Sell Stock 2
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
res=0;
for(int i=0;i<n-1;i++)
{
if(prices[i]<prices[i+1])
res+=prices[i+1]-prices[i];
}
return res;
};
Best Time to Buy and Sell Stock系列
标签:bsp logs size time toc -- res tor nbsp
原文地址:http://www.cnblogs.com/xlqtlhx/p/7750421.html