diff --git a/best-time-to-buy-and-sell-stock/kimyoung.js b/best-time-to-buy-and-sell-stock/kimyoung.js new file mode 100644 index 000000000..35518bcd1 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/kimyoung.js @@ -0,0 +1,21 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + let left = 0, right = 1; + let max = 0; + + while(right < prices.length) { + if(prices[right] < prices[left]) { + left = right; + } else { + max = Math.max(max, prices[right] - prices[left]); + } + right++; + } + return max; +}; + +// time - O(n) iterate through prices once +// space - O(1)