首页 > 代码库 > LeetCode "Best Time to Buy and Sell Stock II"
LeetCode "Best Time to Buy and Sell Stock II"
No limit to transaction count, so it is a recursive sum calculation. Take care of boundary condition.
class Solution {public: int maxProfit(vector<int> &prices) { size_t len = prices.size(); if (len <= 1) return 0; else if (len == 2) { if (prices[0] >= prices[1]) return 0; else return prices[1] - prices[0]; } int len1 = len / 2; int len2 = len - len1; vector<int> v1; v1.assign(prices.begin(), prices.begin() + len1 + 1); int prof1 = maxProfit(v1); vector<int> v2; v2.assign(prices.begin() + len1, prices.end()); int prof2 = maxProfit(v2); return prof1 + prof2; }};
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。