You intend to advance your career as a car dealer. However, as of right now, you just have a room in your garage for exactly one car.
Now, you want to make the maximum profit in a period of ten days by buying and selling a new model at the market. Your assistant has predicted that the market price of the car shall vary as follows:
Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Price | 11 | 7 | 10 | 9 | 13 | 14 | 10 | 15 | 12 | 10 |
What is the maximum profit you can make?
Details and Assumptions:
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Let v ( k , 1 ) and v ( k , 0 ) denote the maximum profit that can be gained starting from day k onwards when currently I do have a car and don't have a car, respectively. Let the price vector be denoted by p the horizon length by n . Then obviously, v ( n , 1 ) = p ( n ) , v ( n , 0 ) = 0 . Furthermore, for 1 ≤ m < n , we have the following DP recursions v ( m , 0 ) = max { − p ( m ) + v ( m + 1 , 1 ) , v ( m + 1 , 0 ) } , and, v ( m , 1 ) = max { p ( m ) + v ( m + 1 , 0 ) , v ( m + 1 , 1 ) } . The above gives an O ( n ) Dynamic Programming Algorithm for computing the maximum profit starting from day 1 (i.e., v ( 1 , 0 ) ), which turns out to be 1 3 for the given example.