Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Why selection algorithm fails:</strong> <br>Note that using selection algorithm is linear in the #elements. Assuming #requests is linear in #elements - it will lead to a quadric solution, and this is why you get time limit exceeded. </p> <p><strong>An alternative approach:</strong> Maintain two heaps: a max heap <code>H1</code> and a min heap <code>H2</code></p> <p>The max heap (<code>H1</code>) will hold the 2/3 lowest numbers while the min heap will gold the 1/3 highest numbers.</p> <p>Now, for each element <code>x</code> you read, check if you need to increase the top 1/3 heap (<code>H2</code>), and if you do: you need to add <code>max{x,H1.max()}</code>. If you added <code>H1.max()</code> - you need to remove it from the heap, and insert <code>x</code> instead. If no addition is needed, check if <code>x</code> is bigger then <code>H2.min()</code>, if it is, remove the min form <code>H2</code>, insert it to <code>H1</code>, and add <code>x</code> to <code>H1</code>.</p> <p><br>Note: the number you are looking for in this solution can be found in <code>O(1)</code> at any time, it is the minimum of the min heap (<code>H2</code>).</p> <p>Total complexity if this solution is <code>O(nlogn + k)</code> - where <code>n</code> is the number of numbers, and <code>k</code> is the total number of "tell the number" requests.</p> <p><strong>Note</strong>: a simpler solution (though probably slower) will be to maintain the list sorted (in a <a href="http://en.wikipedia.org/wiki/Binary_search_tree" rel="nofollow">BST</a> or a <a href="http://en.wikipedia.org/wiki/Skip_list" rel="nofollow">skiplist</a> for example), and use binary search to look for the relevant element.</p> <p><strong>Example:</strong></p> <pre><code>init: H1 = {} H2 = {} input1: H1 = {1} H2 = {} input7: H1={1,7} H2 = {} input 9: //need to add a max, in this case: x &gt; H2.max() -&gt; add x to H2. H1 = {1,7} H2 = {9} tell the number H2.min() = 9 input 21: // 21&gt;9 -&gt; remove H2.min() add it to H1, add x to H2 H1 = {1,7,9} H2 = {21} input 8: H1 = {1,7,8,9} H2 = {21} input 5: //remove max from H1, add max to H2, add x to H1 H1 = {1,7,5,8} H2 = {9,21} tell the number H2.min() = 9 input 11: //remove min from H2, add it to H1, add x to H2. H1 = {1,7,5,8,9} H2 = {11,21} tell the number H2.min() = 11 </code></pre>
    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.
    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