mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
14 lines
364 B
C
14 lines
364 B
C
|
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;
|
||
|
}
|
||
|
};
|