Posted onEdited onInLeetCodeDisqus: Symbols count in article: 250Reading time ≈1 mins.
greedy O(n) time O(1) space 只要挣钱(后一天比前一天价格高)就买进卖出
1 2 3 4 5 6 7 8 9 10 11
classSolution { public: intmaxProfit(vector<int>& prices){ int n = prices.size(); int res = 0; for (int i = 1; i < n; ++i) { res += max(prices[i] - prices[i - 1], 0); } return res; } };