Interview Query

Maximum Profit

Start Timer

0:00:00

Upvote
6
Downvote
Save question
Mark as completed
View comments (3)
Next question

Write a Python function called max_profit that takes a list of integers, where the i-th integer represents the price of a given stock on day i, and returns the maximum profit you can achieve by buying and selling the stock.

You may complete, at most, two complete buy/sell transactions to maximize profits on a stock.

Input:

prices = [3, 3, 5, 0, 0, 3, 1, 4]

Output: 6

Explanation:

Buy on day 4 (price = 0) and sell on day 6 (price = 3): profit = 3 - 0 = 3. Then buy again on day 7 (price = 1) and sell on day 8 (price = 4): profit = 4 - 1 = 3. Total profit: 3 + 3 = 6.

.
.
.
.
.


Comments

Loading comments