Tianhe Gao

LC 121. Best Time to Buy and Sell Stock

 1/**
 2 * @param {number[]} prices
 3 * @return {number}
 4 */
 5var maxProfit = function(prices) {
 6  if (prices.length === 0) return 0
 7  let min = prices[0]
 8  let max = 0
 9  for (let p of prices) {
10    min = Math.min(min, p)
11    max = Math.max(max, p - min)
12  }
13  return max
14};

No notes link to this note

Welcome to tell me your thoughts via "email"
UP