get_max_profit
by Anton Bagayev
JavaScript
let get_max_profit = (prices) => {
let currmin = prices[0];
let currmax = prices[0];
let maxprofit = 0;
for (let i = 1; i < prices.length; i++) {
if (prices[i] <= currmin){
currmin = prices[i];
currmax = prices[i];
//console.log("less " + currmax);
} else {
currmax = prices[i];
//console.log("more " + currmax);
if ((currmax - currmin) > maxprofit) {
maxprofit = currmax - currmin;
}
}
}
return maxprofit;
}
let prices = [10, 7, 5, 8, 11, 9];
let res = get_max_profit(prices);
document.getElementById("demo").innerHTML = res;