diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 0000000..501375b --- /dev/null +++ b/Problem1.py @@ -0,0 +1,27 @@ +# https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ + +# Two pointer approach + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + profit = 0 + i = 0 + + while i < len(prices)-1: + if prices[i] < prices[i+1]: + profit += prices[i+1] - prices[i] + i += 1 + + return profit + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + profit = 0 + i = 0 + + for i in range(1, len(prices)): + if prices[i-1] < prices[i]: + profit += prices[i] - prices[i-1] + i += 1 + + return profit \ No newline at end of file