Getting the lowest possible sum from numbers' difference

后端 未结 10 1325
滥情空心
滥情空心 2020-12-23 22:19

I have to find the lowest possible sum from numbers\' difference.

Let\'s say I have 4 numbers. 1515, 1520, 1500 and 1535. The lowest sum of difference is 30, becaus

10条回答
  •  再見小時候
    2020-12-23 22:31

    Step 1: Calculate pair differences

    I think it is fairly obvious that the right approach is to sort the numbers and then take differences between each adjacent pair of numbers. These differences are the "candidate" differences contributing to the minimal difference sum. Using the numbers from your example would lead to:

    Number Diff
    ====== ====
    1561
            11
    1572
             0
    1572
            37
    1609
            73
    1682
            49
    1731
             0
    1731
           310
    2041
    

    Save the differences into an array or table or some other data structure where you can maintain the differences and the two numbers that contributed to each difference. Call this the DiffTable. It should look something like:

    Index Diff Number1 Number2
    ===== ==== ======= =======
      1     11    1561    1572
      2      0    1572    1572
      3     37    1572    1609
      4     73    1609    1682
      5     49    1682    1731
      6      0    1731    1731
      7    310    1731    2041
    

    Step 2: Choose minimal Differences

    If all numbers had to be chosen, we could have stopped at step 1 by choosing the number pair for odd numbered indices: 1, 3, 5, 7. This is the correct answer. However, the problem states that a subset of pairs are chosen and this complicates the problem quite a bit. In your example 3 differences (6 numbers = 3 pairs = 3 differences) need to be chosen such that:

    • The sum of the differences is minimal
    • The numbers participating in any chosen difference are removed from the list.

    The second point means that if we chose Diff 11 (Index = 1 above), the numbers 1561 and 1572 are removed from the list, and consequently, the next Diff of 0 at index 2 cannot be used because only 1 instance of 1572 is left. Whenever a Diff is chosen the adjacent Diff values are removed. This is why there is only one way to choose 4 pairs of numbers from a list containing eight numbers.

    About the only method I can think of to minimize the sum of the Diff above is to generate and test.

    The following pseudo code outlines a process to generate all 'legal' sets of index values for a DiffTable of arbitrary size where an arbitrary number of number pairs are chosen. One (or more) of the generated index sets will contain the indices into the DiffTable yielding a minimum Diff sum.

    /* Global Variables */
    M = 7    /* Number of candidate pair differences in DiffTable */
    N = 3    /* Number of indices in each candidate pair set (3 pairs of numbers) */
    AllSets = [] /* Set of candidate index sets (set of sets) */
    
    call GenIdxSet(1, []) /* Call generator with seed values */
    
    /* AllSets now contains candidate index sets to perform min sum tests on */
    
    end
    
    procedure: GenIdxSet(i, IdxSet)
      /* Generate all the valid index values for current level */
      /* and subsequent levels until a complete index set is generated */
      do while i <= M
         if CountMembers(IdxSet) = N - 1 then  /* Set is complete */
            AllSets = AppendToSet(AllSets, AppendToSet(IdxSet, i))
         else                                  /* Add another index */
           call GenIdxSet(i + 2, AppendToSet(IdxSet, i))
         i = i + 1
         end
    return
    

    Function CountMembers returns the number of members in the given set, function AppendToSet returns a new set where the arguments are appended into a single ordered set. For example AppendToSet([a, b, c], d) returns the set: [a, b, c, d].

    For the given parameters, M = 7 and N = 3, AllSets becomes:

    [[1 3 5]
     [1 3 6]  <= Diffs = (11 + 37 + 0) = 48
     [1 3 7]
     [1 4 6]
     [1 4 7]
     [1 5 7]
     [2 4 6]
     [2 4 7]
     [2 5 7]
     [3 5 7]]
    

    Calculate the sums using each set of indices, the one that is minimum identifies the required number pairs in DiffTable. Above I show that the second set of indices gives the minimum you are looking for.

    This is a simple brute force technique and it does not scale very well. If you had a list of 50 number pairs and wanted to choose the 5 pairs, AllSets would contain 1,221,759 sets of number pairs to test.

提交回复
热议问题