Search code examples
algorithmknapsack-problemminimizemaximize

Maximize Payout & Keep Cost Below X


Lets say I have 100 nodes each consisting of (payout, cost)

Is there an algorithm to find X nodes that produce the most payout without cost going above Y amount?

I'm not sure if there's a simple sorting algorithm or somehow make a weighted tree out of it. Or is brute forcing it the only approach?

Example:
We want best payout from 3 nodes without going over 20 cost

Nodes[] = (10, 8), (7, 8), (6, 7), (5, 3), (11, 14)

Best Result: (10, 8), (7, 8), (5, 3)
Payout = 22
Cost = 19


Solution

  • This is a famous problem called the 0/1 knapsack problem, and there are a ton of approaches you can use for solving it. The easiest - and one of the most efficient - solutions is a dynamic programming algorithm that considers the elements one at a time. With that term in mind, I think you should be able to find some great resources here on Stack Overflow or more broadly across the internet.