← switch

122. Best Time to Buy and Sell Stock II

medium

Array · Dynamic Programming · Greedy

read the problem on leetcode →

first solved 13 Aug 2024 · 2 accepted submissions

python
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        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 
        
python
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        max = 0
        start = prices[0]
        len1 = len(prices)
        for i in range(0 , len1):
            if start < prices[i]: 
                max += prices[i] - start
            start = prices[i]
        return max