LeetCode: Best Time To Buy And Sell Stock Solution
1var maxProfit = function (prices) {2 let prefixMins = []3 let min = Infinity4 let max = 056 for (const price of prices) {7 min = Math.min(min, price)8 prefixMins.push(min)9 }1011 for (let i = 1; i < prices.length; i++) {12 max = Math.max(max, prices[i] - prefixMins[i - 1])13 }1415 return max16}
Comments
Loading comments...
Tags
leetcode
array
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.