浅谈动态规划以及相关的股票问题 (3)

最后返回 secSell 。

代码实现 class Solution { public int maxProfit(int[] prices) { int fstBuy = Integer.MIN_VALUE, fstSell = 0; int secBuy = Integer.MIN_VALUE, secSell = 0; for(int i = 0; i < prices.length; i++) { fstBuy = Math.max(fstBuy, -prices[i]); fstSell = Math.max(fstSell, fstBuy + prices[i]); secBuy = Math.max(secBuy, fstSell - prices[i]); secSell = Math.max(secSell, secBuy + prices[i]); } return secSell; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfzgj.html