Solution Review: Buy and Sell Stock

This lesson contains the solution review for the challenge of finding the maximum profit generated by buying and selling stocks!

In this lesson, we will review the solution to the problem in the last exercise. Let’s go over the problem statement once again:

Given an array of numbers consisting of daily stock prices, calculate the maximum amount of profit that can be made from buying on one day and selling on another day.

We consider two approaches to this problem. In the first, we consider a brute force approach that solves the problem in O(N2)O(N^2), where NN is the size of the array of numbers. We then improve upon this solution to take our solution to a time complexity of O(N)O(N).

Solution 1: Brute Force

Implementation

Get hands-on with 1200+ tech skills courses.