Question: We are given a sequence of positive integers a1,... , an corresponding to the prices of a stock in times 1,, n. We want to

We are given a sequence of positive integers a1,... , an corresponding to the prices of a stock in times 1,, n. We want to decide when to buy the stock and when to sell it. More pre- cisely we want to find i j such that aj-ai s maximized. For example if the sequence is (30,10, 5,9,6,11,10), then the optimal strategy is to buy at time 3 (at the price of 5) and sell at time 6 (at the price of 11) (a) (3 points) Describe a simple O(n2) solution to this problem. (b) (12 points) Improve the running time by using a divide-and-conquer recursive algorithm and analyze its correctness and running time
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
