-
Notifications
You must be signed in to change notification settings - Fork 0
/
Best Time to Buy and Sell Stock.py
36 lines (29 loc) · 1.18 KB
/
Best Time to Buy and Sell Stock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#Divide and Conquer Strategy (O(NLogN) )
class Solution:
def getMax(self,i,j,prices):
return max(prices[i:j+1])
def getMin(self,i,j,prices):
return min(prices[i:j+1])
def divideandconquer(self,low,high,prices):
if(low>=high):
return 0
mid=(low+high)//2
left=self.divideandconquer(low,mid,prices) #get the transaction from left subarray
right=self.divideandconquer(mid+1,high,prices) #get the transaction from right subarray
both=self.getMax(mid+1,high,prices)-self.getMin(low,mid,prices) #buy min price from left and sell max price from right
return max(both,left,right)
def maxProfit(self, prices: List[int]) -> int:
#Let's use Divide and Conquer Strategy
n=len(prices)
return self.divideandconquer(0,n-1,prices)
#O(N) Approach!
class Solution:
def maxProfit(self, prices: List[int]) -> int:
bestMin=prices[0]
n=len(prices)
maxProfit=0 #no transactions
for i in range(1,n):
profit=prices[i]-bestMin
maxProfit=max(maxProfit,profit)
bestMin=min(prices[i],bestMin)
return maxProfit