支持html5的网站,贵阳免费做网站,青岛中企动力做网站怎么样,贵阳网站建设电话Leecode 123.买卖股票的最佳时机 III 123.买卖股票的最佳时机III 123.买卖股票的最佳时机III 题目地址#xff1a;力扣#xff08;LeetCode#xff09;官网 - 全球极客挚爱的技术成长平台 题目类型#xff1a;股票问题 class Solution {
public:int maxProfit(vector… Leecode 123.买卖股票的最佳时机 III 123.买卖股票的最佳时机III 123.买卖股票的最佳时机III 题目地址力扣LeetCode官网 - 全球极客挚爱的技术成长平台 题目类型股票问题 class Solution {
public:int maxProfit(vectorint prices) {int n prices.size();// dp[i][j]代表在第i的状态为j// j取值0还没买过股票1持有第一只股票2卖出第一支股票3持有第二支股票4卖出第二支股票vectorvectorint dp(n, vectorint(5));dp[0][1] -prices[0];dp[0][3] -prices[0];for (int i 1; i n; i) {dp[i][1] max(dp[i - 1][0] - prices[i], dp[i - 1][1]);dp[i][2] max(dp[i - 1][1] prices[i], dp[i - 1][2]);dp[i][3] max(dp[i - 1][2] - prices[i], dp[i - 1][3]);dp[i][4] max(dp[i - 1][3] prices[i], dp[i - 1][4]);}return dp[n - 1][4];}
}; Leecode 188.买卖股票的最佳时机 IV 题目地址力扣LeetCode官网 - 全球极客挚爱的技术成长平台 题目类型股票问题 class Solution {
public:int maxProfit(int k, vectorint prices) {int n prices.size();vectorvectorint dp(n, vectorint(2 * k 1, 0));for (int i 1; i 2 * k; i 2) {dp[0][i] -prices[0]; }for (int i 1; i n; i) {for (int j 1; j 2 * k; j 2) {dp[i][j] max(dp[i - 1][j - 1] - prices[i], dp[i - 1][j]);dp[i][j 1] max(dp[i - 1][j] prices[i], dp[i - 1][j 1]);}}return dp[n - 1][2 * k];}
};