Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem. Our goal is to buy and sell a given stock with maximum profit, assuming (not quite realistically) that we are able to predict the
Problem. Our goal is to buy and sell a given stock with maximum profit, assuming (not quite realistically) that we are able to predict the future. To be more specific, we assume we have an array P[1.. (n 2 such that for each i , Pi is the price 0) of the stock on day i; our goal is to compute an array A[Lm] of actions where each action is either NONE, BUY, or SELL, we require that such a sequence of actions is valid; that is: e the Buys and SELLs are alternating: between two BuY actions there is always a SELL action, and between two SELL actions there is always a BUY action; e we cannot sell before we buy: before a SELL. action there is always a Buy action e "cool down requirement": a SELL cannot be immediately followed by a BUY. If a valid sequence has a BUY without any subsequent SELL we say that it is incpmplete; otherwise it is complete. It is easy to see that an optimal valid sequence must be complete. As our running example, consider the prices In that case, an (the) optimal sequence of actions is NONE BUY NONNE SELL | NONE BUY | SELL | NONE which will give a profit of (5-2) +(3-2)4 Instead, we shall solve the problem using dynamic programming. We shall employ 2 tables, B1.n . for i 1n, Bi is the maximum profit possible after a valid but incomplete sequence of fori 0.., Vl is the maximum profit possible after a valid and complete sequence of actions and V10..n), with intended meaning: actions Al1 Alil (thus B-P Your remaining tasks are .(6p) Write, and justify, the recurrences for computing (for i E 2.n) the values of Bi and Vli, given the values of B and V on smaller i. 5. (6p) Using the recurrences from step 4, write a dynamic programming algorithm to fill the arrays B1.n and V1n] (you may assume your language has a Max operator)
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