Best Time to Buy and Sell Stock II
Problem Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
This is not a DP question
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# Don't miss any opportunity to make profit.
# Watch pepcoding's video for this if you don't understand
n=len(prices)
buyingdate=0
sellingdate=0
profit=0
for i in range(1,n):
if(prices[i]>=prices[i-1]):
sellingdate+=1
else:
profit=profit+prices[sellingdate]-prices[buyingdate]
buyingdate=i
sellingdate=i
# The below line is for the last corner case
profit=profit+prices[sellingdate]-prices[buyingdate]
return profit
Last updated