力扣第121、160题

121.买股票的最佳时机

image.png
贪心思想,取最左最小的,再取右边相差区间最大

  1. var maxProfit = function(prices) {
  2. let low = Infinity
  3. let res = 0
  4. for(let i = 0; i<prices.length;i++){
  5. low = Math.min(low,prices[i])
  6. res = Math.max(res,prices[i]-low)
  7. }
  8. return res;
  9. };