题解 | #买卖股票的最好时机(一)#
买卖股票的最好时机(一)
http://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec
class Solution {
public:
/**
*
* @param prices int整型vector
* @return int整型
*/
int maxProfit(vector<int>& prices) {
// write code here
int ans = 0, minp = INT_MAX;
for (int i = 0; i < prices.size(); i++) {
if (prices[i] >= minp) {
ans = max(ans, prices[i] - minp);
}
minp = min(minp, prices[i]);
}
return ans;
}
};