-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxProfit.py
61 lines (49 loc) · 1.82 KB
/
maxProfit.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
MaxProfit Class!
Finds a maximum achievable profit
"""
import sys
class MaxProfit():
def __init__(self, prices):
self.prices = prices
self.numberOfDays = len(prices)
def maxProfitOneTransactionLinear(self):
"""
Time: O(N)
Space: O(1)
Returns:
[int]: max profit with 1 transaction
"""
bestPriceToBuy, bestDayToBuy = sys.maxsize, 0
bestPriceToSell, bestDayToSell = -sys.maxsize, 0
numberOfBetterPricesForBuying = 1
for day, price in enumerate(self.prices):
if (price < bestPriceToBuy):
bestDayToBuy = day
bestPriceToBuy = price
numberOfBetterPricesForBuying += 1
elif (price > bestPriceToSell):
bestDayToSell = day
bestPriceToSell = price
if (numberOfBetterPricesForBuying == self.numberOfDays):
return 'Unprofittable'
print(f"Bought on Day#{bestDayToBuy+1} and Sold on Day#{bestDayToSell+1}")
return bestPriceToSell - bestPriceToBuy
def maxProfitOneTransactionQuadratic(self):
"""
Time: O(N^2)
Space: O(1)
Returns:
[int]: max profit with 1 transaction
"""
curMaxProfit = 0
for i in range(0, len(self.prices)-1):
for j in range(i+1, len(self.prices)):
diff = self.prices[j] - self.prices[i]
if(diff > curMaxProfit):
curMaxProfit = diff
return curMaxProfit
if __name__ == "__main__":
obj = MaxProfit([10,9,8,7,6,5,4,6,7,8,9])
print(obj.maxProfitOneTransactionLinear())
# print(obj.maxProfitOneTransactionQuadratic())