```
class Solution {
public:
int maxProfit(vector<int>& prices) {
int cost = 2147483647; // max int
int profit = 0;
for (int i = 0;i<prices.size();++i)
{
int p = prices[i] - cost;
if (p > profit)
profit = p;
if (prices[i] < cost)
cost = prices[i];
}
return profit;
}
};
```