#include <cstdio> class Solution { public: /** * * @param prices int整型vector * @return int整型 */ int maxProfit(vector<int>& prices) { // write code here int min = prices[0]; int max = prices[0]; int i = 0; int value = 0; while (i < prices.size()) { if (prices[i]>max) { max = prices[i]; } if (prices[i]<min) { min = prices[i]; } if (max == prices[i]&&i+1>=prices.size()) { value+=max-min; } else if (max == prices[i]&&prices[i+1]<prices[i]) { value+=max-min; max = prices[i+1]; min = prices[i+1]; } i++; } return value; } };