原题地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
解决方法:动态规划,minimun存储的是当前价格中最小的。
class Solution { public: int maxProfit(vector<int>& prices) { int minimum = INT_MAX, ret = 0, size = prices.size(); for(int i = 0; i < size; ++i){ if(prices[i] < minimum) minimum = prices[i]; int diff = prices[i] - minimum; if(diff > ret) ret = diff; } return ret; } };