@requires_authorization @author Johnsondu@create_time2015.7. +: on@url [best time to buy and sell stock] (https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)/************************ * @description: Dynamic programming. * The adjacent elements are poor and then converted to the maximum continuous subsequence and the * @time_complexity: O (n) * @ Space_complexity:o (n) ************************/classSolution { Public:intMaxprofit ( vector<int>& prices) {intP_size = Prices.size ();if(P_size <2)return 0; vector<int>dp for(inti =1; i < p_size; i + +) Dp.push_back (Prices[i]-prices[i-1]);intAns =0;intCur =0;intDp_size = Dp.size (); for(inti =0; i < dp_size; i + +) {cur = cur + dp[i];if(Cur <0) cur =0;if(cur > ans) ans = cur; }returnAns }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode" 121. best time to Buy and Sell Stock