题意: 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 };