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
给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。
i
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
The text was updated successfully, but these errors were encountered:
/** * @param {number[]} prices * @return {number} */ var maxProfit = function(prices) { if (prices.length === 0) { return 0; } const hold = [-prices[0]]; const sell = [-Infinity]; const wait = [0]; for (let i = 1; i < prices.length; i++) { hold[i] = Math.max(hold[i - 1], wait[i - 1] - prices[i]); sell[i] = hold[i - 1] + prices[i]; wait[i] = Math.max(sell[i - 1], wait[i - 1]); } return Math.max(sell[prices.length - 1], wait[prices.length - 1]); };
function maxProfit(prices: number[]): number { if (prices.length === 0) { return 0; } const hold = [-prices[0]]; const sell = [-Infinity]; const wait = [0]; for (let i = 1; i < prices.length; i++) { hold[i] = Math.max(hold[i - 1], wait[i - 1] - prices[i]); sell[i] = hold[i - 1] + prices[i]; wait[i] = Math.max(sell[i - 1], wait[i - 1]); } return Math.max(sell[prices.length - 1], wait[prices.length - 1]); };
Sorry, something went wrong.
✨ Tcdian/keep#251
7cb3762
No branches or pull requests
309. Best Time to Buy and Sell Stock with Cooldown
给定一个整数数组,其中第
i
个元素代表了第i
天的股票价格 。设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
Example
The text was updated successfully, but these errors were encountered: