31 lines
634 B
C++
Raw Normal View History

2014-10-20 11:23:39 +08:00
// Source : https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/
// Author : Hao Chen
// Date : 2014-06-18
class Solution {
public:
int maxProfit(vector<int> &prices) {
int max=0, begin=0, end=0, delta=0;
for (int i=0; i<prices.size(); i++) {
end = i;
delta = prices[end] - prices[begin];
if (delta <= 0){
begin = i;
}
if ( delta > max ){
max = delta;
}
}
return max;
}
};