/*** say you have an array for which the ith element is the price of a given stock on day I. * design an algorithm to find the maximum profit. you may complete as your transactions as you like * (ie, buy one and every one share ofthe stock multiple times ). * However, you may not engage in multiple transactions at the same time (ie, you must wait the stock before you buy again ). ** subject requirements can be bought and sold multiple times, At the same time, there can be only one stock in your hand. That is to say, the stock in the hand must be sold in the next purchase. Buying and selling can happen in the same * subscript of the vector. For example, the input vector is [6, 9, 12, 8, 4, 11, 2, 1, 9]. you can buy and sell a vector [I] at the same time. * Therefore, the profit is (-6 + 9) + (-9 + 12) + (-4 + 11) + (-1 + 9) * In this way, you can buy before each ascending subsequence and sell at the end of the ascending subsequence. It is equivalent to obtaining the benefits of all ascending subsequences. */Class solution {public: int maxprofit (vector & prices) {int I = 0; int size = prices. size (); If (size <2) {return 0;} int totalprofit = 0; for (I = 1; I Prices [I-1]) {totalprofit + = Prices [I]-Prices [I-1] ;}} return totalprofit ;}};
leetcode best time to buy and stock Stock II