function maxProfit(prices) {
let minPrice = prices[0];
let total = 0;
for (let i = 0; i < prices.length; i++) {
let num = prices[i];
let sum = num - minPrice
if (num < minPrice) {
minPrice = num;
}if(sum > total){
total = sum
}if(num > minPrice && )
}
return total
}
console.log(maxProfit([7, 1, 5, 3, 6, 4])); // 7
console.log(maxProfit([1, 2, 3, 4, 5])); // 4
console.log(maxProfit([7, 6, 4, 3, 1])); // 0
Code will run automatically after changes.
Code runs in an isolated environment.
Code runs in an isolated environment.