diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index 259fff34..8ad6c8c6 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -313,6 +313,26 @@ func max(a,b int)int { } ``` +JavaScript: + +```javascript +const maxProfit = prices => { + const len = prices.length; + // 创建dp数组 + const dp = new Array(len).fill([0, 0]); + // dp数组初始化 + dp[0] = [-prices[0], 0]; + for (let i = 1; i < len; i++) { + // 更新dp[i] + dp[i] = [ + Math.max(dp[i - 1][0], -prices[i]), + Math.max(dp[i - 1][1], prices[i] + dp[i - 1][0]), + ]; + } + return dp[len - 1][1]; +}; +``` +