0

There is a well-known problem of the best time to buy and sell stock. Assume now we have two arrays, SELL and BUY. Each time SELL[i] > BUY[i]. Assume we have initial budget B, and we can buy any amount of stock bounded by our budget. We want to determine the maximum profit we can reach using the best strategy.

I've tried to use a greedy approach:

  1. Set sell_price as MAX_FLOAT

  2. while BUY[i] < sell_price: update sell_price as current minimum SELL[:i] and increase i. If BUY[i] > sell_price. Buy maximum stocks with sell_price and immediately sell them by BUY[i], then update budget and calculate profit.

  3. Repeat 1-2.

I am unsure if this greedy approach is the best. It seems to be knapsack problem, but I guess it's much simplier.

openspace
  • 151
  • 7

0 Answers0