Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You are consulting for a small investment company. They give you the price of Google s shares over the last n days. Let S [
You are consulting for a small investment company. They give you the price of Googles shares over
the last n days. Let S n be the array of share prices, where S 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 and S then you should return buy on day and sell on day
Your goal is to design a divideandconquer algorithm for this problem that runs in time On log n
a points In one sentence, describe a simple algorithm for this problem that takes On time.
b points Now design a DivideandConquer algorithm and justify its correctness. Con
sider two cases: there is an optimal solution in which the investors are holding the stock
at the end of day n and there is no such optimal solution. For each case, express the
optimal solution in terms of something you can compute on the two subarrays.
c points Analyze the running time of your algorithm.
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