From b6cefbf933313fc3706c852008509db6d8457db3 Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Tue, 14 Apr 2026 15:06:57 -0700 Subject: [PATCH] Problem1 added --- Problem1.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Problem1.py 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