Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions problems/solved/easy/best_time_to_buy_and_sell_stock.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
def maxProfit(prices):
"""
:type prices: List[int]
:rtype: int
"""
"""Compute the maximum profit that can be obtained by buying and selling a stock at most once."""


def compute_max_profit(prices: list[int]) -> int:
length = len(prices)
total_profit = 0
max_profit = 0

for i in range(length-1):
marginal_profit = prices[i+1] - prices[i]
for i in range(length - 1):
marginal_profit = prices[i + 1] - prices[i]
total_profit = total_profit + marginal_profit
if total_profit < 0:
total_profit = 0
max_profit = max(max_profit, total_profit)

return max_profit

print(maxProfit([7,1,5,3,6,4]))
print(maxProfit([7,6,4,3,1]))

print(compute_max_profit([7, 1, 5, 3, 6, 4]))
print(compute_max_profit([7, 6, 4, 3, 1]))