M
Martin Hart Turner
Hi:
I am looking for a 'best-fit' algorithm for a (small) list of numbers. Allow
me to explain.
Imagine I have a list is integers as so: 1, 2, 3, 1, 5, 8, 1, 12, 2, 1. You
can see there are repeats in the list.
Given another integer number, I want to know which numbers from the list,
when summed, give me the requested value. For instance, if I want to find
which numbers make the sum of 7, the algorithm could return 1+2+3+1 or 5+2
or 2+3+2. Returning an array of integers that point to the candidates in the
list would suffice as the return value, for instance in the previous example
I would get [0,1,2,3] or [4,1] or [1,2,8]. I only need one sequence
returned, not all possible combinations. Ideally, the solution would be the
shortest route e.g. the combination with the least number of elements in the
returned array (of list).
Can anyone give me some pointers as to how I might go about this task?
TIA,
Martin.
I am looking for a 'best-fit' algorithm for a (small) list of numbers. Allow
me to explain.
Imagine I have a list is integers as so: 1, 2, 3, 1, 5, 8, 1, 12, 2, 1. You
can see there are repeats in the list.
Given another integer number, I want to know which numbers from the list,
when summed, give me the requested value. For instance, if I want to find
which numbers make the sum of 7, the algorithm could return 1+2+3+1 or 5+2
or 2+3+2. Returning an array of integers that point to the candidates in the
list would suffice as the return value, for instance in the previous example
I would get [0,1,2,3] or [4,1] or [1,2,8]. I only need one sequence
returned, not all possible combinations. Ideally, the solution would be the
shortest route e.g. the combination with the least number of elements in the
returned array (of list).
Can anyone give me some pointers as to how I might go about this task?
TIA,
Martin.