Skip to Main Content

Best Time to Buy and Sell Stock

Problem URL:Best Time to Buy and Sell Stock

My Solution

JavaScript

function maxProfit(prices: number[]): number {
  let min = prices[0],
    max = 0;

  for (let price of prices) {
    if (price < min) {
      min = price;
    }
    if (price - min > max) {
      max = price - min;
    }
  }

  return max;
}

Let's Connect

Twitter GitHub LinkedIn