122. Best Time to Buy and Sell Stock II
Difficulty: Easy
Say you have 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 may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
1 | **Input:** [7,1,5,3,6,4] |
Example 2:
1 | **Input:** [1,2,3,4,5] |
Example 3:
1 | **Input:** [7,6,4,3,1] |
解题思路
注意边界条件的判断,当到达数组末尾后,如果持有股票需要卖出
Solution
1 | class Solution: |