0

So I came across this question during an interview and was unable to solve it. Hopefully, someone can advise. The problem is this.

Imagine you have S amounts of savings in integers. You are thinking of buying stocks. Someone has provided you with 2 N-sized arrays of stock purchase prices, and their sell prices the following day. Write an algorithm that is able to take in S and the 2 arrays, and return the maximum profits that you are able to achieve the following day. Note that the lengths of the 2 arrays are equal. The number at index i in the first array shows the purchase price of the ith stock and the number at index i in the second array shows the sell price of the ith stock.

eg. S = 10, buy_price = [4, 6, 5], sell_price = [6, 10, 7], your savings are 10, there are 3 stock options. The first option the purchasing price is 4, and the selling price is 6 the next day. The second option purchasing price is 6 and the selling price is 10 the next day. So on and so forth. The maximum profit here is 6 where you buy the stock options that cost 4 and 6. and sell them the next day. Your function should return 6 here.

My initial approach was finding the profits/cost ratio of each stock and sorting them. However, this may not lead to the most ideal stock purchases. For example, for this case S = 10, buy_price = [6, 5, 5], sell_price = [12, 9, 8], the best option is not to buy the stock that cost 6 even though it has the highest profits/cost ratio (you can't buy anything with your remaining 4 savings) but to buy the other 2 stock options for a maximum profit of 7.

Does anyone have any idea of how to tackle this problem? Thank you!

whiterook6
  • 3,270
  • 3
  • 34
  • 77
David Yao
  • 107
  • 8
  • 2
    Isn't this the knapsack problem? – גלעד ברקן Jan 18 '21 at 14:22
  • 1
    Agree with @גלעדברקן, since the profits on each stock are fixed, this is a knapsack problem – Abhinav Mathur Jan 18 '21 at 14:38
  • 1
    Just to add a tiny bit of clarity to the above comment this is [the weighted knapsack problem](https://en.wikipedia.org/wiki/Knapsack_problem) with weights given by the stock buy price and values given by stock sell price - stock buy price. – mrmcgreg Jan 18 '21 at 15:07
  • Yup you guys are right, found the explanation and implementation [here](https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/), thanks ;) – David Yao Jan 18 '21 at 16:15

2 Answers2

2

If we consider the prices to be weights and the profits to be value, this problem is exactly the 0/1 knapsack problem. This problem is weakly NP-complete. That is, there is no polynomial time algorithm as a function of the size of the input, but with dynamic programming you can solve this problem in polynomial time as a function of the budget.

Thus, if the budget is reasonably small, this problem is efficiently solvable with a dynamic programming approach.

ADdV
  • 1,162
  • 2
  • 16
  • Yup you are right, I found a nice explanation and implementation [here](https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/) – David Yao Jan 18 '21 at 16:14
1

I think the problem should be able to be solved by integer programming, which can be set up by using the Excel solver. I think the problem itself can't be solved by a greedy algorithm, but please correct me if I am wrong.

Yulian

Yulian
  • 69
  • 3
  • 3
    1. Looks more like a comment than an answer 2. Why would you need integer programming? – Abhinav Mathur Jan 18 '21 at 14:35
  • I should have given more information. As ADdv mentioned, this is a 0/1 knapsack problem, which can also be solved by integer programming. I mentioned IP solver in Excel because it is very easy to set up an IP for this problem in Excel. – Yulian Jan 19 '21 at 18:20