Question
Were looking at the price of a given stock over n consecutive days, numbered i = 1, 2, . . . , n. For each
Were looking at the price of a given stock over n consecutive days, numbered i = 1, 2, . . . , n. For each day i, we have a price p(i) per share for the stock on that day. (Well assume for simplicity that the price was fixed during each day.) Wed like to know: How should we choose a day i on which to buy the stock and a later day j > i on which to sell it, if we want to maximize the profit per share, p(j) p(i)? (If there is no way to make money during the n days, we should conclude this instead.) Show an algorithm to find the optimal numbers i and j int time O(n)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started