Algorithm to find elements best fitting in a particular amount

后端 未结 5 812
遇见更好的自我
遇见更好的自我 2021-01-17 03:32

I\'m looking for an algorithm to solve the follwoing problem (I will explain it with an example):

Let say I have 10.000 $ available amount and following costs I can

相关标签:
5条回答
  • 2021-01-17 03:34

    I designed an algorithm like this in java some weeks ago. I used a binary matrix with width N, where N is the number of different costs, and the height will be 2^N, giving us all possible combinations.

    So the first item would refer to the lowest cost in your case. Here is an example with 1000, 3000, 4000. (You can expand this, but I just want to show you how I did.)

    Width = N = 3

    Height = 2^N = 8

    0 0 0 = 0       +   0       +   0       =   0
    1 0 0 = 1000    +   0       +   0       =   1000
    0 1 0 = 0       +   3000    +   0       =   3000
    1 1 0 = 1000    +   3000    +   0       =   4000
    0 0 1 = 0       +   0       +   4000    =   4000
    1 0 1 = 1000    +   0       +   4000    =   5000
    0 1 1 = 0       +   3000    +   4000    =   7000
    1 1 1 = 1000    +   3000    +   4000    =   8000
    

    I had a list with the costs in it. And as i went down the matrix row by row, i just checked the list where there was a 1 on the matrix. Since the costs are sorted, the row i find that is equal or greater than the total amount will be the lowest possible.

    Tell me if you don't understand and i'll elaborate!

    0 讨论(0)
  • 2021-01-17 03:41

    Your problem is a NP problem; there is no known solution to this problem that will guarantee to find an optimal solution within an acceptable time frame. The only way to really find an optimal solution would be to try all possible solutions and then check which one is optimal. If you have thousands of costs, though, this would be awfully slow, of course.

    All other solutions that are faster than the naive approach mentioned above, are not guaranteed to find an optimal solution. They may find a good solution, yet you can never say for sure if a better solution was possible or not.

    It is the same problem as the following: You have a truck that can transport X pounds of goods and you have goods of different weights. Your goal is to make optimal use of the available capacity. This one is also known as the "knapsack problem". There are no better solutions known other than those listed on this page. None of them can guarantee an optimal result, though.

    0 讨论(0)
  • 2021-01-17 03:46

    You can sort the list and pick the top until your budget is depleted. It's also used in bin-packing and it's guarantee to be within some range of the optimum.

    0 讨论(0)
  • 2021-01-17 03:48

    This is a simple dynamic programming problem (A variation of Knapsack). State can be defined as [position][rest_amount][how_many_bills_can_be_paid]. A recursive solution below:

    assuming Cis the array of cost and memo is initialized with -1:

    const int N = 10;    //number of notes to pay
    int memo[N][M][K];   //remember to initialize it with -1
    
    int func(int cur_index,int rest_amount,int K){
    
        if(cur_index == N){
            return 0;
        }
    
        int &ret = memo[cur_index][rest_amount][K];
        if(ret != -1) return ret;    //memoization ensures we won't solve the same sub problem more than once
        ret = 0;
        if(rest_amount >= C[cur_index]  && K > 0 )
        ret = func(cur_index+1,cost+C[cur_index],K-1);
    
        ret = max(ret,func(cur_index+1,cost,K);
    
        return ret;
    }
    
    0 讨论(0)
  • 2021-01-17 03:56

    I can't improve your solution for general data, however there are few improvements which can speed it up.

    If total amount N is quite low there is simple dynamic solution:

    setOfValues
    put totalAmount to setOfValues
    foreach cost do
        foreach amount in setOfValues do
            if (amount - cost) > 0 and not exists (amount - cost) in setOfValues
                put (amount - cost) to setOfValues
    get lowest setOfValues
    

    You can simple upgrade it to additional requirements (like up to x costs).

    0 讨论(0)
提交回复
热议问题