-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBest Time to Buy and Sell Stock.py
37 lines (26 loc) · 1.28 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
37
# https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
class Solution:
def maxProfit(self, prices: List[int]) -> int:
"""
OBJECTIVE: You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Time Complexity: O(n) where n = length of prices. A while-loop was created to iterate the loop once
Space Complexity: O(1) because no additional space was used
"""
# Get length of list
n = len(prices)
# Create pointers
leftPtr = 0
rightPtr = 1
# Create variable to hold max profit
maxProfit = 0
# Iterate list
while rightPtr < n:
# If selling price is higher than buying, then calculate max profit and update rightPtr
if prices[leftPtr] < prices[rightPtr]:
maxProfit = max(maxProfit, prices[rightPtr] - prices[leftPtr])
rightPtr += 1
# If selling price isn't higher than buying, then move leftPtr to rightPtr and update rightPtr
else:
leftPtr = rightPtr
rightPtr += 1
return maxProfit