20220421, 3min

|
```javascript /**

  • @param {number[]} prices
  • @return {number} */ // 13:23 -> 13:26 // 贪心:左取最小,右取最大 var maxProfit = function(prices) { let low = Number.MAX_VALUE; let profit = 0; for (let price of prices) { low = Math.min(low, price); profit = Math.max(profit, price - low) } return profit }; ``` | | —- |

20220420, 4min

|
```javascript // 08:49 -> 08:53 // 贪心,左取最小值,右取最大值 var maxProfit = function(prices) { let low = Number.MAX_VALUE; let result = 0; for (let i = 0; i < prices.length; i++) { low = Math.min(low, prices[i]); result = Math.max(result, prices[i] - low) } return result; };

  1. |
  2. | --- |
  3. <a name="qrXrc"></a>
  4. ## 20220419, 2min
  5. | <br />```javascript
  6. // 13:45 -> 13:47
  7. //
  8. var maxProfit = function(prices) {
  9. let low = Number.MAX_VALUE;
  10. let result = 0;
  11. for (let i = 0; i < prices.length; i++) {
  12. low = Math.min(low, prices[i]);
  13. result = Math.max(result, prices[i] - low);
  14. }
  15. return result;
  16. };

| | —- |