LeedCode --- Best Time to Buy and Sell Stock

3217 단어 code
제목 링크
제목: find the maximum positive difference between the price on the ith day and the jth day
부착된 코드:
 1 class Solution {  2 public:  3     int maxProfit(vector<int> &prices) {  4         if (prices.size() == 0)  5              return 0;  6         // "minimum" holds the minimum price before the ith day.  7         // "max_diff" holds the maximum difference between prices[i] and prices[j]  8         // where 0 <= i < j < prices.size()

 9         int minimum = prices[0], max_diff = 0; 10         for (unsigned int i = 1; i < prices.size(); i++) { 11             if (prices[i] - minimum > max_diff) { 12                 max_diff = prices[i] - minimum; 13  } 14             if (prices[i] < minimum) { 15                 minimum = prices[i]; 16  } 17  } 18         return max_diff; 19  } 20 };

좋은 웹페이지 즐겨찾기