标签:
class Solution { public: int maxProfit(vector<int>& prices) { //eg: 5 6 2 3 1 4; // 记录i之前最小的元素; int min = INT_MAX; int pro = 0; int n=prices.size(); for(int i=0;i<n;i++){ if(prices[i]-min >pro) pro=prices[i]-min; else if(prices[i] < min) min = prices[i]; } return pro; } };
Best Time to Buy and Sell Stock
标签:
原文地址:http://www.cnblogs.com/julie-yang/p/4671485.html