class Solution { public: int maxProfit(vector<int>& prices) { int min = prices[0], max = 0, best = 0; for(int i = 1; i < prices.size(); i++) { if(prices[i] < min) { min = prices[i]; continue; } int n = prices[i] - min; if(best < n) { max = prices[i]; best = n; } } return best; } };
class Solution { public: int maxNum(int a, int b) { if(a > b) return a; return b; } int maxProfit(vector<int>& prices) { int min = prices[0], max = 0, best = 0; for(int i = 1; i < prices.size(); i++) { if(prices[i] < min) { min = prices[i]; continue; } best = maxNum(best, prices[i]-min); } return best; } };
class Solution { public: int maxProfit(vector<int>& prices) { int lsf = INT_MAX; int op = 0; int pist = 0; for(int i = 0; i < prices.size(); i++){ if(prices[i] < lsf){ lsf = prices[i]; } pist = prices[i] - lsf; if(op < pist){ op = pist; } } return op; } };