Maximum Subarray Problem: Difference between revisions
Jump to navigation
Jump to search
Line 11: | Line 11: | ||
The maximum subarray problem is useful to solve the following problem: assuming we have access to stock prices of a company for a number of days, and the price does not change during a day, determine what would have been the best day to buy and the best day to sell one unit of stock to make the maximum profit, for the entire interval we have data for. For example, if we maintain the stock prices in an array, for 4 days (0-3) as follows {10, 8, 12, 11} then we would have made the biggest profit of 4 by buying on day 1 at 8 and selling on day 2 at 12. | The maximum subarray problem is useful to solve the following problem: assuming we have access to stock prices of a company for a number of days, and the price does not change during a day, determine what would have been the best day to buy and the best day to sell one unit of stock to make the maximum profit, for the entire interval we have data for. For example, if we maintain the stock prices in an array, for 4 days (0-3) as follows {10, 8, 12, 11} then we would have made the biggest profit of 4 by buying on day 1 at 8 and selling on day 2 at 12. | ||
The problem has an obvious O(n<sup>2</sup>) | The problem has an obvious [[#O.28n2.29_Brute_Force_Approach|O(n<sup>2</sup>) brute force approach]] solution, an O(<font color=red>?</font>) [[#Divide-and-Conquer|divide-and-conquer]] solution and an [[#O.28n.29_Iterative_Solution|O(n) iterative solution]]. | ||
=O(n<sup>2</sup>) Brute Force Approach= | =O(n<sup>2</sup>) Brute Force Approach= |
Revision as of 22:11, 9 August 2018
External
- CLRS page 68
Internal
Overview
The maximum subarray problem is useful to solve the following problem: assuming we have access to stock prices of a company for a number of days, and the price does not change during a day, determine what would have been the best day to buy and the best day to sell one unit of stock to make the maximum profit, for the entire interval we have data for. For example, if we maintain the stock prices in an array, for 4 days (0-3) as follows {10, 8, 12, 11} then we would have made the biggest profit of 4 by buying on day 1 at 8 and selling on day 2 at 12.
The problem has an obvious O(n2) brute force approach solution, an O(?) divide-and-conquer solution and an O(n) iterative solution.