标签:fit class value ret tor nbsp max blog return
方法:记录便利过程中的best profit
class Solution { public: int maxProfit(vector<int>& prices) { if(prices.size() < 2) return 0; int profit = 0, minValue = prices[0]; for(int i=0; i<prices.size(); ++i) { profit = max(profit, prices[i] - minValue); if(prices[i] < minValue) minValue = prices[i]; } return profit; } };
Best Time to Buy and Sell Stock II
class Solution { public: int maxProfit(vector<int>& prices) { int sum = 0; for(int i=1; i<prices.size(); ++i) { if(prices[i] - prices[i-1] > 0) sum += prices[i] - prices[i-1]; } return sum; } };
Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock II
标签:fit class value ret tor nbsp max blog return
原文地址:http://www.cnblogs.com/chengyuz/p/7040437.html