Skip to content

Commit

Permalink
feat: stock price maximization
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwin-nair98 committed Mar 6, 2024
1 parent 305f2e9 commit cd01ef9
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions elements/5_6_Stock_Profit_Maximize/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Question: Given a list of stock prices, find the maximum profit that can be made by buying
# and selling one unit of stock


def maxPrice(stocks):
minUntilNow = stocks[0]
maxProfit = 0

for i in range(1, len(stocks)):
if stocks[i] < minUntilNow:
minUntilNow = stocks[i]
else:
maxProfit = max(maxProfit, stocks[i] - minUntilNow)
return maxProfit


def expect(a, b):
result = a == b
print("Test for " + str(a) + (" passed!" if result else " failed!"))


a = [310, 315, 275, 295, 260, 270, 290, 230, 255, 250]
maxProfit = 30

expect(maxProfit, maxPrice(a))

0 comments on commit cd01ef9

Please sign in to comment.