We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 中等
Related Topics: 数组, 动态规划
给定一个整数数组prices,其中第 prices[i] 表示第 _i_ 天的股票价格 。
prices
prices[i]
_i_
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
**注意:**你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
输入: prices = [1,2,3,0,2] 输出: 3 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
示例 2:
输入: prices = [1] 输出: 0
提示:
1 <= prices.length <= 5000
0 <= prices[i] <= 1000
Language: JavaScript
/** * @param {number[]} prices * @return {number} */ var maxProfit = function(prices) { const n = prices.length const dp = new Array(n).fill(0).map(() => new Array(2).fill(0)) // -1天需要的参数 dp[-1] = [0] dp[0] = [0, -prices[0]] // 从第一天开始 for (let i = 1; i < n; i++) { dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i]) dp[i][1] = Math.max(dp[i-1][1], dp[i-2][0] - prices[i]) } // 返回最后一天不持有股票 return dp[n-1][0] }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
309. 最佳买卖股票时机含冷冻期
Description
Difficulty: 中等
Related Topics: 数组, 动态规划
给定一个整数数组
prices
,其中第prices[i]
表示第_i_
天的股票价格 。设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
**注意:**你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
示例 2:
提示:
1 <= prices.length <= 5000
0 <= prices[i] <= 1000
Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: