cpp-interview/Problems/LeetcodeProblems/121-best-time-to-buy-and-sell-stock.h

14 lines
364 B
C
Raw Normal View History

2018-02-09 21:47:58 +08:00
class Solution {
public:
int maxProfit(vector<int>& prices) {
int left = 0, right = 0;
int max = 0, temp = 0;
for(auto i = 0; i < prices.size(); ++i) {
right = i;
temp = prices[right] - prices[left];
if(temp < 0) left = i;
if(max < temp) max = temp;
}
return max;
}
};