何时买卖股票才能最大化收益,LeetCode买卖股票II策略详解?

2026-04-03 01:511阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计153个文字,预计阅读时间需要1分钟。

何时买卖股票才能最大化收益,LeetCode买卖股票II策略详解?

无限制交易次数,因此是一个递归求和计算。注意边界条件。类解决方案如下:

javapublic int maxProfit(vector prices) { // 省略具体实现细节}

Nolimittotransactioncount,soitisarecursivesumcalculation.Takecareofboundarycondition.class

No limit to transaction count, so it is a recursive sum calculation. Take care of boundary condition.

何时买卖股票才能最大化收益,LeetCode买卖股票II策略详解?

class Solution {public: int maxProfit(vector if (len = prices[1]) return 0; else return prices[1] - prices[0]; } int len1 = len / 2; int len2 = len - len1; vector v1; v1.assign(prices.begin(), prices.begin() + len1 + 1); int prof1 = maxProfit(v1); vector v2; v2.assign(prices.begin() + len1, prices.end()); int prof2 = maxProfit(v2); return prof1 + prof2; }};

LeetCode "Best Time to Buy and Sell Stock II",,

LeetCode "Best Time to Buy and Sell Stock II"

本文共计153个文字,预计阅读时间需要1分钟。

何时买卖股票才能最大化收益,LeetCode买卖股票II策略详解?

无限制交易次数,因此是一个递归求和计算。注意边界条件。类解决方案如下:

javapublic int maxProfit(vector prices) { // 省略具体实现细节}

Nolimittotransactioncount,soitisarecursivesumcalculation.Takecareofboundarycondition.class

No limit to transaction count, so it is a recursive sum calculation. Take care of boundary condition.

何时买卖股票才能最大化收益,LeetCode买卖股票II策略详解?

class Solution {public: int maxProfit(vector if (len = prices[1]) return 0; else return prices[1] - prices[0]; } int len1 = len / 2; int len2 = len - len1; vector v1; v1.assign(prices.begin(), prices.begin() + len1 + 1); int prof1 = maxProfit(v1); vector v2; v2.assign(prices.begin() + len1, prices.end()); int prof2 = maxProfit(v2); return prof1 + prof2; }};

LeetCode "Best Time to Buy and Sell Stock II",,

LeetCode "Best Time to Buy and Sell Stock II"