Problem:
Say you has an array for which the i-th element is the price of a given-stock on day I.
Design an algorithm to find the maximum profit. You could complete as many transactions as (ie, buy one and sell one share of the stock multiple times). However, engage in multiple transactions for the same time (ie, you must sell the stock before you buy again).
Hide TagsArray GreedyTest instructions: Given a stock price list, first buy after the sale, can have multiple transactions, to seek maximum benefits
Thinking:
In fact, it is to ask for an array of two number of the difference and the maximum value, the use of greedy strategy, money earned will not let go!!! Revenue is the biggest.
Code
Class Solution {public: int Maxprofit (vector<int>& prices) { int n=prices.size (); int profit=0; for (int i=1;i<n;i++) if (prices[i]-prices[i-1]>0) profit+=prices[i]-prices[i-1]; return profit;} ;
Leetcode | | 122. Best time to Buy and Sell Stock II