标签:price com turn www. amp details rand ret article
https://www.cnblogs.com/grandyang/p/4997417.html
https://blog.csdn.net/qq508618087/article/details/51671504
用buy、sell两个数组表达,注意初始化
class Solution { public: int maxProfit(vector<int>& prices) { int length = prices.size(); if(length <= 0) return 0; vector<int> buy(length+1,0),sell(length+1,0); buy[1]=-prices[0]; for(int i = 2;i <= length;i++){ buy[i] = max(buy[i-1],sell[i-2] - prices[i-1]); sell[i] = max(sell[i-1],buy[i-1] + prices[i-1]); } return buy[length] > sell[length] ? buy[length] : sell[length]; } };
309. Best Time to Buy and Sell Stock with Cooldown
标签:price com turn www. amp details rand ret article
原文地址:https://www.cnblogs.com/ymjyqsx/p/10490818.html