Skip to content

Latest commit

 

History

History
21 lines (21 loc) · 376 Bytes

121.买卖股票的最佳时机.md

File metadata and controls

21 lines (21 loc) · 376 Bytes
/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
  // dp[i] = dp[i-1] + i;
  let min = Number.MAX_SAFE_INTEGER,
    res = 0;
  for (let i = 0; i < prices.length; i++) {
    let cur = prices[i];
    if (cur < min) {
      min = cur;
    }
    if (cur - min > res) {
      res = cur - min;
    }
  }
  return res;
};