LC 121. Best Time to Buy and Sell Stock

/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function(prices) {
  if (prices.length === 0) return 0
  let min = prices[0]
  let max = 0
  for (let p of prices) {
    min = Math.min(min, p)
    max = Math.max(max, p - min)
  }
  return max
};
欢迎通过「邮件」或者点击「这里」告诉我你的想法
Welcome to tell me your thoughts via "email" or click "here"