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 many transactions as you like
(ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time
(ie, you must sell the stock before you buy again).
解法1:O(N)
既然是可以交易任意多次,只要price[i] > price[i - 1]我们就认为是一次合格的交易,
那么最大的profit一定是所有positive profit的和。只要扫描一遍求出所有正差值的和即为答案
这是一种greedy的思路。