class Solution {
public int maxProfit(int[] prices) {
int maxProfit = 0;
for (int i = 0; i < prices.length - 1; i++) {
for (int j = i + 1; j < prices.length; j++) {
if (prices[j] - prices[i] > maxProfit) {
maxProfit = prices[j] - prices[i];
}
}
}
return maxProfit;
}
}
class Solution {
public int maxProfit(int[] prices) {
int profit = 0, cost = Integer.MAX_VALUE;
for (int price : prices) {
cost = Math.min(cost, price);
profit = Math.max(profit, price - cost);
}
return profit;
}
}