Skip to content
New issue

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

leetcode 123. 买卖股票的最佳时机 III #动态规划 #12

Open
webVueBlog opened this issue May 27, 2022 · 1 comment
Open

leetcode 123. 买卖股票的最佳时机 III #动态规划 #12

webVueBlog opened this issue May 27, 2022 · 1 comment

Comments

@webVueBlog
Copy link
Collaborator

image
123. 买卖股票的最佳时机 III

@webVueBlog
Copy link
Collaborator Author

123. 买卖股票的最佳时机 III

/*
 * @lc app=leetcode.cn id=123 lang=javascript
 *
 * [123] 买卖股票的最佳时机 III
 */

// @lc code=start
/**
 * @param {number[]} prices
 * @return {number}
获取的最大利润,最多可以完成两笔交易。
// i第几天 k第几笔 j持有/未持有
dp[3][0][0] = max(dp[2][0][0], dp[2][-1][1] + prices[3]);
dp[3][0][1] = max(dp[2][0][0] - prices[3], dp[2][0][1]);
 (732 ms)
 */
var maxProfit = function(prices) {
 const n = prices.length;
 const dp = new Array(n).fill(0).map(() => {
  return new Array(3).fill(0).map(() => {
   return new Array(2).fill(0)
  })
 })

 // 转移状态
 for(let i = 0; i < n; i++) {
  // 最多两笔
  for(let k = 0; k <= 2; k++) {
   if(i === 0) {
    dp[i][k][0] = 0
    dp[i][k][1] =  -prices[i]
    continue;
   }
   if(k === 0) {
    // 0笔交易,没有持有股票
    dp[i][k][0] = 0;
   } else {
    dp[i][k][0] = Math.max(dp[i-1][k][0], dp[i-1][k-1][1] + prices[i]);
   }
   dp[i][k][1] = Math.max(dp[i-1][k][0] - prices[i], dp[i-1][k][1]);
  }
 }
 // 最后一天两笔未持有
 return dp[n-1][2][0];
};
// @lc code=end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant