Speculating Prices

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:

  • There is no other hidden cost than the market prices listed above.
  • You can only buy and sell at the market prices. The difference is the profit.
  • You can hold at most one car in the garage at a time.
  • You can perform at most one transaction in one day.

Inspired by IARCS


The answer is 13.

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.

1 solution

Abhishek Sinha
Aug 8, 2017

Let v ( k , 1 ) v(k,1) and v ( k , 0 ) v(k,0) denote the maximum profit that can be gained starting from day k k onwards when currently I do have a car and don't have a car, respectively. Let the price vector be denoted by p \mathbf{p} the horizon length by n n . Then obviously, v ( n , 1 ) = p ( n ) , v ( n , 0 ) = 0. v(n,1)=p(n), v(n,0)=0. Furthermore, for 1 m < n 1\leq m <n , we have the following DP recursions v ( m , 0 ) = max { p ( m ) + v ( m + 1 , 1 ) , v ( m + 1 , 0 ) } , 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 ) } . v(m,1)=\max \{p(m)+v(m+1,0), v(m+1,1) \}. The above gives an O ( n ) O(n) Dynamic Programming Algorithm for computing the maximum profit starting from day 1 (i.e., v ( 1 , 0 ) v(1,0) ), which turns out to be 13 13 for the given example.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...