Commit d9548ae3 authored by yaozc's avatar yaozc
Browse files

keep going

parent beb751fa
......@@ -1660,14 +1660,25 @@ public:
Best Time to Buy and Sell Stock
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
class Solution {
public:
int maxProfit(vector<int>& prices) {
int min = INT_MAX;
int profit = 0;
for(int i=0; i<prices.size(); i++){
if(prices[i] < min){
min = prices[i];
}
else{
profit = std::max(profit, prices[i] - min);
}
}
return profit;
}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment