101 Logo
onenoughtone

Solution Approach

Solution Overview

There are 3 main approaches to solve this problem:

  1. One-Pass Approach - Complex approach
  2. Kadane's Algorithm Approach - Complex approach
  3. Brute Force Approach - Complex approach

Approach 1: One-Pass Approach

The key insight for solving this problem efficiently is to track the minimum price seen so far and calculate the potential profit if we sell at the current price.

As we iterate through the array, we update the minimum price if we find a lower price, and we update the maximum profit if selling at the current price would yield a higher profit.

This approach allows us to find the maximum profit in a single pass through the array, with O(n) time complexity and O(1) space complexity.

Algorithm:

  1. Initialize minPrice to infinity (or the maximum possible value) and maxProfit to 0.
  2. Iterate through each price in the array:
  3. a. Update minPrice to the minimum of minPrice and the current price.
  4. b. Calculate the potential profit if we sell at the current price: currentProfit = currentPrice - minPrice.
  5. c. Update maxProfit to the maximum of maxProfit and currentProfit.
  6. Return maxProfit as the result.

Time Complexity:

O(n)

Where n is the length of the prices array. We iterate through the array once, performing constant-time operations at each step.

Space Complexity:

O(1)

We only use a constant amount of extra space regardless of the input size.

Approach 2: Kadane's Algorithm Approach

Another way to think about this problem is in terms of daily price changes. We can transform the problem into finding the maximum subarray sum of the differences between consecutive prices.

This approach is based on Kadane's algorithm, which is used to find the maximum subarray sum. However, in this case, we have the additional constraint that we can only buy once and sell once, and we must buy before selling.

By tracking the maximum profit ending at each position, we can find the overall maximum profit in a single pass through the array.

Algorithm:

  1. Initialize maxCurr (maximum profit ending at current position) to 0 and maxSoFar (maximum profit found so far) to 0.
  2. Iterate through the array starting from the second element (i = 1):
  3. a. Calculate the price difference: diff = prices[i] - prices[i-1].
  4. b. Update maxCurr to the maximum of 0 and maxCurr + diff.
  5. c. Update maxSoFar to the maximum of maxSoFar and maxCurr.
  6. Return maxSoFar as the result.

Time Complexity:

O(n)

Where n is the length of the prices array. We iterate through the array once, performing constant-time operations at each step.

Space Complexity:

O(1)

We only use a constant amount of extra space regardless of the input size.

Approach 3: Brute Force Approach

The brute force approach is to consider all possible pairs of buy and sell days, where the buy day comes before the sell day, and find the pair that maximizes the profit.

While this approach is straightforward to understand, it has a time complexity of O(n²), which is inefficient for large inputs. However, it's worth mentioning as a baseline solution.

Algorithm:

  1. Initialize maxProfit to 0.
  2. Use nested loops to consider all possible pairs of buy and sell days:
  3. a. Outer loop (i) from 0 to n-2 represents the buy day.
  4. b. Inner loop (j) from i+1 to n-1 represents the sell day.
  5. c. Calculate the profit: profit = prices[j] - prices[i].
  6. d. Update maxProfit to the maximum of maxProfit and profit.
  7. Return maxProfit as the result.

Time Complexity:

O(n²)

Where n is the length of the prices array. We consider all possible pairs of buy and sell days, which is O(n²).

Space Complexity:

O(1)

We only use a constant amount of extra space regardless of the input size.

Pseudocode

solution.pseudo
1
2
3
4
5
6
7
8
9
10
function maxProfit(prices):
minPrice = Infinity
maxProfit = 0
for price in prices:
minPrice = min(minPrice, price)
currentProfit = price - minPrice
maxProfit = max(maxProfit, currentProfit)
return maxProfit
ProblemSolutionCode
101 Logo
onenoughtone