Best Time to Buy and Sell Stock
class Solution { public: int maxProfit(vector<int>& prices) { if(prices.empty())return 0; int parent=0; int son=0; for(int i=0;i<prices.size()-1;i++){ son=max(son+prices[i+1]-prices[i],0); parent=max(parent,son); } return parent; } };
这道题是典型的局部最优和全局最优问题,son不断地更新,parent取parent和son的最大值