The difference with previous one is: this time we can buy and sell the share multiple times. We can use greedy to solve this problem. Every time, if we have a local minimum, we buy it and sell it at the local maximum. The idea is equal to: buy one when next day its price rise and sell it in the next day. This is easier to implement.
int maxProfit(vector<int>& prices) {
if(prices.empty()) return 0;
int len=prices.size();
int sum=0;
for(int i=1;i<len;i++)
{
if(prices[i-1]<prices[i]) sum+=prices[i]-prices[i-1];
}
return sum;
}