Find subset with elements that are furthest apart from eachother

前端 未结 4 2052
野的像风
野的像风 2021-02-03 10:39

I have an interview question that I can\'t seem to figure out. Given an array of size N, find the subset of size k such that the elements in the subset are the furthest apart fr

相关标签:
4条回答
  • 2021-02-03 10:53
    $length = length($array);
    sort($array); //sorts the list in ascending order
    $differences = ($array << 1) - $array; //gets the difference between each value and the next largest value
    sort($differences);  //sorts the list in ascending order
    $max = ($array[$length-1]-$array[0])/$M; //this is the theoretical max of how large the result can be
    $result = array();
    for ($i = 0; i < $length-1; $i++){
        $count += $differences[i];
        if ($length-$i == $M - 1 || $count >= $max){ //if there are either no more coins that can be taken or we have gone above or equal to the theoretical max, add a point
            $result.push_back($count);
            $count = 0;
            $M--;
        }
    }
    return min($result)
    

    For the non-code people: sort the list, find the differences between each 2 sequential elements, sort that list (in ascending order), then loop through it summing up sequential values until you either pass the theoretical max or there arent enough elements remaining; then add that value to a new array and continue until you hit the end of the array. then return the minimum of the newly created array.

    This is just a quick draft though. At a quick glance any operation here can be done in linear time (radix sort for the sorts).

    For example, with 1, 4, 7, 100, and 200 and M=3, we get:

    $differences = 3, 3, 93, 100
    $max = (200-1)/3 ~ 67
    then we loop:
    $count = 3, 3+3=6, 6+93=99 > 67 so we push 99
    $count = 100 > 67 so we push 100
    min(99,100) = 99
    

    It is a simple exercise to convert this to the set solution that I leave to the reader (P.S. after all the times reading that in a book, I've always wanted to say it :P)

    0 讨论(0)
  • 2021-02-03 10:57

    I suppose your set is ordered. If not, my answer will be changed slightly.

    Let's suppose you have an array X = (X1, X2, ..., Xn)
    
    Energy(Xi) = min(|X(i-1) - Xi|, |X(i+1) - Xi|), 1 < i <n
    
    j <- 1
    while j < n - k do
        X.Exclude(min(Energy(Xi)), 1 < i < n)
        j <- j + 1
        n <- n - 1
    end while
    
    0 讨论(0)
  • 2021-02-03 11:07

    This can be solved in polynomial time using DP.

    The first step is, as you mentioned, sort the list A. Let X[i,j] be the solution for selecting j elements from first i elements A.

    Now, X[i+1, j+1] = max( min( X[k,j], A[i+1]-A[k] ) ) over k<=i.

    I will leave initialization step and memorization of subset step for you to work on.

    In your example (1,2,6,10) it works the following way:

        1    2   6   10
    1   -    -   -    -
    2   -    1   5    9
    3   -    -   1    4
    4   -    -   -    1
    
    0 讨论(0)
  • 2021-02-03 11:15

    The basic idea is right, I think. You should start by sorting the array, then take the first and the last elements, then determine the rest.

    I cannot think of a polynomial algorithm to solve this, so I would suggest one of the two options.

    One is to use a search algorithm, branch-and-bound style, since you have a nice heuristic at hand: the upper bound for any solution is the minimum size of the gap between the elements picked so far, so the first guess (evenly spaced cells, as you suggested) can give you a good baseline, which will help prune most of the branches right away. This will work fine for smaller values of k, although the worst case performance is O(N^k).

    The other option is to start with the same baseline, calculate the minimum pairwise distance for it and then try to improve it. Say you have a subset with minimum distance of 10, now try to get one with 11. This can be easily done by a greedy algorithm -- pick the first item in the sorted sequence such that the distance between it and the previous item is bigger-or-equal to the distance you want. If you succeed, try increasing further, if you fail -- there is no such subset.

    The latter solution can be faster when the array is large and k is relatively large as well, but the elements in the array are relatively small. If they are bound by some value M, this algorithm will take O(N*M) time, or, with a small improvement, O(N*log(M)), where N is the size of the array.

    As Evgeny Kluev suggests in his answer, there is also a good upper bound on the maximum pairwise distance, which can be used in either one of these algorithms. So the complexity of the latter is actually O(N*log(M/k)).

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