Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are consulting for a small investment company. They give you the price of Googles shares over the last n days. Let S[1 . .

You are consulting for a small investment company. They give you the price of Googles shares over the last n days. Let S[1 . . . n] be the array of share prices, where S[1] is the price of the stock on the first day in this time period. During this window, the company wanted to buy a fixed number of shares on some day and sell all these shares on some later day (that is, only one purchase and one sale). The company wants to know when they should have bought and when they should have sold in order to maximize their profit. If there was no way to make money during the n days, you should report Impossible.

For example, if n = 3 and S = [9, 1, 5], then you should return buy on day 2 and sell on day 3.

Your goal is to design a divide-and-conquer algorithm for this problem that runs in time O(n log n).

(a.) In one sentence, describe a simple algorithm for this problem that takes O(n 2 ) time.

(b.) Use the same divide strategy as merge sort. Consider two cases: (1) there is an optimal solution in which the investors are holding the stock at the end of day n/2, and (2) there is no such optimal solution. For each case, express the optimal solution in terms of something you can compute on the two subarrays. Give a brief explanation and pseudocode for your algorithm.

(c.) Analyze the running time of your algorithm.

(d.) How could we improve this algorithm to O(n)? (Do not fundamentally change the approach from Divide & Conquer)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Robb,Carlos Coronel

5th Edition

061906269X, 9780619062699

More Books

Students also viewed these Databases questions