0122.cpp (363B)
1 class Solution { 2 public: 3 int maxProfit(vector<int> &prices) { 4 int profit = 0; 5 prices.push_back(INT_MIN); 6 for (int i = 0, j = 0; i < prices.size() - 1; i++) { 7 while (prices[j] < prices[j + 1]) 8 j++; 9 profit += prices[j] - prices[i]; 10 i = j++; 11 } 12 return profit; 13 } 14 };