LeetCode_Best Time to Buy and Sell Stock II

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 (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
2
3
4
5
6
7
8
9
10
11
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if not prices:
return 0

max_profit = 0
for i in range(1, len(prices)):
if prices[i] > prices[i-1]:
max_profit += prices[i] - prices[i-1]

return max_profit