本文主要是介绍Leetcode122买股票的最佳时机2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码:
class Solution {public int maxProfit(int[] prices) {int n = prices.length;int[][] dp = new int[n][2];dp[0][0] = 0;dp[0][1] = -prices[0];for(int 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-1][0]-prices[i]);}return dp[n-1][0];}
}
这篇关于Leetcode122买股票的最佳时机2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!