Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This solution works with non-integer weights and uses constant space (ie: space complexity = O(1)). It does, however modify the input array, but the only difference in the end is that the elements will be in a different order.</p> <ul> <li><p>Add the weight of each input to the weight of the following input, starting from the bottom working your way up. Now each weight is actually the sum of that input's weight and all of the previous weights.</p></li> <li><p>sum_weights = the sum of all of the weights, and n = N.</p></li> <li><p>K times:</p> <ul> <li><p>Choose a random number r in the range [0,sum_weights)</p></li> <li><p>binary search the first n elements for the first slot where the (now summed) weight is greater than or equal to r, i.</p></li> <li><p>Add input[i].id to output.</p></li> <li><p>Subtract input[i-1].weight from input[i].weight (unless i == 0). Now subtract input[i].weight from to following (> i) input weights and also sum_weight.</p></li> <li><p>Move input[i] to position [n-1] (sliding the intervening elements down one slot). This is the expensive part, as it's O(N) and we do it K times. You can skip this step on the last iteration.</p></li> <li><p>subtract 1 from n</p></li> </ul></li> <li><p>Fix back all of the weights from n-1 down to 1 by subtracting the preceding input's weight</p></li> </ul> <p>Time complexity is O(K*N). The expensive part (of the time complexity) is shuffling the chosen elements. I suspect there's a clever way to avoid that, but haven't thought of anything yet.</p> <h2>Update</h2> <p>It's unclear what the question means by "output: K random unique Ids". The solution above assumes that this meant that the output ids are supposed to be unique/distinct, but if that's not the case then the problem is even simpler:</p> <ul> <li><p>Add the weight of each input to the weight of the following input, starting from the bottom working your way up. Now each weight is actually the sum of that input's weight and all of the previous weights.</p></li> <li><p>sum_weights = the sum of all of the weights, and n = N.</p></li> <li><p>K times:</p> <ul> <li><p>Choose a random number r in the range [0,sum_weights)</p></li> <li><p>binary search the first n elements for the first slot where the (now summed) weight is greater than or equal to r, i.</p></li> <li><p>Add input[i].id to output.</p></li> </ul></li> <li><p>Fix back all of the weights from n-1 down to 1 by subtracting the preceding input's weight</p></li> </ul> <p>Time complexity is O(K*log(N)).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload