-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyarrays_121_bestTimetoBuyandSellStock.py
56 lines (42 loc) · 1.74 KB
/
easyarrays_121_bestTimetoBuyandSellStock.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
"""
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
leetcode
easy
arrays
input : an array prices
output: the maximum profit you can achieve from this transaction.
If you cannot achieve any profit, return 0
Logic : 2 pointer
approach 1 : left right pointer
def maxProfit(prices: List[int]) -> int:
maxGap = 0
left, right = 0, 1
while right < len(prices):
if prices[left] < prices[right]:
maxGap = max(maxGap, prices[right] - prices[left])
right += 1
else:
left, right = right, right + 1
return maxGap
We cannot buy a stock in the future and sell it now in stock market; thus,
we only need to store the lowest stock price in every iteration. And in next iteration,
we only need to check whether current stock price is higher than previous lowest stock price.
If so, then we might need to update maxGap. On the other hand, if current stock price is lower
than previous lowest price, then we need to update two pointers (left, right).
Time Complexity: O(n), Space Complexity: O(1)
approach 2 : min max
Explanation: Instead of using two variables to store the index of the lowest and current
price of stock like solution 1, we only need to store the lowest price as iteration goes on.
If we find current price is less than pervious lowest price, then it is possible that the
maximum profit might change. Therefore, we might need to update the value of maxGap as shown:
Time Complexity: O(n), Space Complexity: O(1)
"""
def maxProfit(prices):
buyprice = float("inf")
maxgap = 0
for price in prices :
buyprice = min(buyprice, price)
maxgap = max(maxgap, price-buyprice)
return maxgap
print(maxProfit([7,1,5,3,6,4]))
print(maxProfit([7,6,4,3,1]))