class Solution {
public:
int maxProfit(vector<int>& prices)
{
int n = prices.size();
int maxProfit = 0;
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; j++)
{
maxProfit = max(maxProfit, prices[j] - prices[i]);
}
}
return maxProfit;
}
};
class Solution {
public:
int maxProfit(vector<int>& prices)
{
int minCost = INT_MAX, maxProfit = 0;
for (int price : prices)
{
minCost = min(price, minCost);
maxProfit = max(maxProfit, price - minCost);
}
return maxProfit;
}
};
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up