LeetCode122
Best Time to Buy and Sell Stock II
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).
与前面一题不同,这里允许多次交易,但是在买入的时候必须卖掉手上的。
思路也很简单,先买第一天的,如果后面一天的价值高,就卖掉手上的,买入第二天的,如果后面一天的价值低,就把手上的股票换成那天的。