The pigs can fly under the tuyere. The bull market in China today is a "missed seven years". Give you an opportunity to review the history of a stock, known for a continuous N-day price movement, in the length of an integer array of n, the array of elements I (Prices[i]) represents the stock price of the first day. Suppose you don't have a stock at first, but you have the opportunity to buy 1 shares and then sell 1 shares at most two times, and make sure you have no stock before you buy. If two trading opportunities are abandoned, the yield is 0. Design algorithms to calculate the maximum benefits you can get. Input value range: 2<=n<=100,0<=prices[i]<=100
Input Example:
3,8,5,1,7,8
Output Example:
12
1#include <iostream>2#include <vector>3 4 using namespacestd;5 6 intCalculatemax (vector<int>prices);7 8 intMain ()9 {Ten intb[6] = {3,8,5,1,7,8}; Onevector<int> A (b, B +6); A -cout << Calculatemax (a) <<Endl; - return 0; the } - - intCalculatemax (vector<int>prices) { - + intLength =prices.size (); - intMAXM =0; + inti,j,counts; A //Dynamic programming is create a form at //just like sub[][]=0 -vector<vector<int> >suboptimization (Length,vector<int> (3,0)); - - for(i =1; i < length; i + +){ - - for(counts =1; Counts <3; Counts + +){ inMAXM = Suboptimization[i-1][counts]; - to for(j =0; J < I; J + + ){ +MAXM = Max (MAXM, Suboptimization[j][counts-1] + prices[i]-prices[j]); - } theSuboptimization[i][counts] =MAXM; * } $ Panax Notoginseng } - the + returnSuboptimization[length-1][2]; A}
Use dynamic planning to do the specific do not explain.
Millet Pen Test (dynamic planning)