Search⌘ K
AI Features

Feature #4: Maximum Profit

Explore how to calculate the maximum profit from a list of daily stock price changes using Python. Understand how to iterate through the data to find the sublist with the highest sum, indicating the best profit or smallest loss. This lesson teaches efficient scanning techniques with O(n) time and O(1) space complexity.

Description

We have now extracted the stock increase and decrease percentages over a number of consecutive days. This will be represented as a list of numbers, one for each consecutive day, holding the increase or decrease in stock price on the given day. We can use this data to find the maximum profit that could have been made for the given time period. Sometimes, the maximum profit might be negative, indicating a period of minimum loss. For simplicity and to avoid fractional values, we are rounding the increase and decrease percentages to their nearest value.

We’ll be provided with a list of positive and negative integers. The indexes will indicate the day number and the integer value will indicate the stock increase or decrease percentage on that day. We have to return the maximum value that can be obtained by adding the sublist elements.

Let’s try to understand this better with an illustration:

Solution

The ...