0121.Best Time to Buy and Sell Stock
class Solution {
public:
int maxProfit(vector<int>& prices) {
int result = 0;
int minPrice = 100000;
for(int& price : prices) {
// find minPrice
// if price is smaller than minPrice, replace minPrice
// else, stock can be selled, compare to the result
if(price < minPrice){
minPrice = price;
} else {
result = max(result, price - minPrice);
}
}
return result;
}
};