Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an algorithm that is <code>O(n + size(smallest subset) * log(n))</code>. If the smallest subset is much smaller than the array, this will be <code>O(n)</code>.</p> <p>Read <a href="http://en.wikipedia.org/wiki/Heap_%28data_structure%29" rel="nofollow">http://en.wikipedia.org/wiki/Heap_%28data_structure%29</a> if my description of the algorithm is unclear (it is light on details, but the details are all there).</p> <ol> <li>Turn the array into a heap arranged such that the biggest element is available in time <code>O(n)</code>.</li> <li>Repeatedly extract the biggest element from the heap until their sum is large enough. This takes <code>O(size(smallest subset) * log(n))</code>.</li> </ol> <p>This is almost certainly the answer they were hoping for, though not getting it shouldn't be a deal breaker.</p> <p><em>Edit:</em> Here is another variant that is often faster, but can be slower.</p> <pre><code>Walk through elements, until the sum of the first few exceeds S. Store current_sum. Copy those elements into an array. Heapify that array such that the minimum is easy to find, remember the minimum. For each remaining element in the main array: if min(in our heap) &lt; element: insert element into heap increase current_sum by element while S + min(in our heap) &lt; current_sum: current_sum -= min(in our heap) remove min from heap </code></pre> <p>If we get to reject most of the array without manipulating our heap, this can be up to twice as fast as the previous solution. But it is also possible to be slower, such as when the last element in the array happens to be bigger than S.</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. 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