Open lpatmo opened 5 years ago
My JS solution:
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
//sliding window
let currentProfit = 0;
let maxProfit = 0;
let b = 0;
let s = 1;
while (b < prices.length-1 && s < prices.length ) {
if (prices[s] < prices[b]) {
b = s;
} else {
currentProfit = prices[s] - prices[b];
maxProfit = Math.max(currentProfit, maxProfit);
}
s++;
}
return maxProfit;
};
Problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock
Example 1:
Example 2: