Let's say I have a Product table in the database that have the following properties of id and price:
Product
=======
ProductId int PRIMARY KEY
Price int
For simplicity reasons, I will put Price as int datatype. So right now I would like to use a random algorithm that will retrieve n number of records from the database whereby the Price will sum up to a fixed amount m.
For example:
- n = 5
- m = 100
So I will retrieve 5 Product records that the Price will add up to 100.
One Possible Result:
ProductId Price
=====================
5 10
109 27
2081 31
139 21
331 11
======================
Sum: 100
I've tried Brute-force approach but it seems to take ages to get the correct sum of 100. Is there any algorithm to do this?
Aucun commentaire:
Enregistrer un commentaire