The idea is to keep a running temporary variable to keep track of minimum and at each time try if there's a profit in selling at current time (and buying when it's minimum we kept track of). Could there be a more elegant solution?

```
class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size()<=1) return 0;
int best_profit = 0;
int prev_floor = prices[0];
for (int i=1; i<prices.size(); i++){
best_profit = max(best_profit, prices[i] - prev_floor);
prev_floor = min(prev_floor, prices[i]);
}
return best_profit;
}
};
```