Question: Problem 5 . [ Category: Divide and Conquer ] [ 3 0 points ] We will work on a very typical problem: Best time to
Problem Category: Divide and Conquer points
We will work on a very typical problem: Best time to buy and sell stock. The simplest version of this
problem is as follows:
You are given an array prices where pricesi is the price of a given stock on the ith day. You want to
maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to
sell that stock. And our GOAL is to maximize the profit you can achieve from this transaction, and return
that profit value. If you cannot achieve any profit, return
Example:
Input: prices
Output:
Explanation: Buy on day price and sell on day price profit
Note that buying on day lowest price and selling on day highest price is not allowed because you
must buy before you sell.
Input: prices
Output:
Explanation: In this case, no transactions are done and the max profit
We can of course solve the problem by brute force method, that is checking every pair of possible buy and
sell day and return the maximum profit, that will give us an algorithm running in quadratic time. But we
can actually do better that. We can solve the problem using two algorithm paradigms: divide and conquer,
dynamic programming. Here we are going to focus on divide and conquer one.
Without loss to generality, we can assume n is some number that of power of if not, then we just let patch
some fakes days to make it up to ni n and the fake days will just have same price as of the nth
day The basic idea is that, the maximum profit will come from either one of the following three cases of
operations:
both buying and selling happen during the first n
days
both buying and selling happen during the last n
days
buying happens during the first n
days and then selling happens during the last n
days.
In the first two cases, we just do recursive call on the procedure to find the answer. For the third case,
we can simply buy at the lowest price from the first n
days, and sell at the highest price from the last n
days, and easily get the profit for this case. Finally, we can just return the maximum value among the three
answers to get the maximum profit for the original problem. The algorithm would be as follows, please fill
the blanks and analyze the running time of the algorithm.
BestTimeBuySellpricesn
if n then
return
end if
maxP rof it
m n
Aprof it BestTimeBuySell
Bprof it BestTimeBuySell
crossBuyP rice minpricesm min function finds the minimum of the given array
crossSellP rice max max function finds the maximum of the given array
Cprof it crossSellP rice crossBuyP rice Cprofit: maximum profit of case
maxP rof it max
return maxP rof it
Analyze the running time T n of the algorithm:
T nT n
Therefore, T n in
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
