Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a design problem, which means they want to see how quickly you can augment existing data-structures.</p> <p>start with what you know: </p> <ul> <li>O(1) update, i.e. insertion/deletion, is screaming <code>hashtable</code></li> <li>O(1) getMin is screaming hashtable too, but this time ordered.</li> </ul> <p>Here, I am presenting one way of doing it. You may find something else that you prefer.</p> <ul> <li>create a HashMap, call it <code>main</code>, where to store all the elements</li> <li>create a LinkedHashMap (java has one), call it <code>mins</code> where to track the minimum values.</li> <li>the first time you insert an element into <code>main</code>, add it to <code>mins</code> as well.</li> <li>for every subsequent insert, if the new value is less than what's at the head of your <code>mins</code> map, add it to the map with something equivalent to <code>addToHead</code>.</li> <li>when you remove an element from <code>main</code>, also remove it from <code>mins</code>. 2*O(1) = O(1)</li> <li>Notice that <code>getMin</code> is simply peeking without deleting. So just peek at the head of <code>mins</code>.</li> </ul> <p>EDIT:</p> <p><a href="http://en.wikipedia.org/wiki/Amortized_analysis" rel="nofollow">Amortized algorithm</a>:</p> <p>(thanks to @Andrew Tomazos - Fathomling, let's have some more fun!)</p> <p>We all know that the cost of insertion into a hashtable is O(1). But in fact, if you have ever built a hash table you know that you must keep doubling the size of the table to avoid overflow. Each time you double the size of a table with n elements, you must re-insert the elements and then add the new element. By this analysis it would seem that worst-case cost of adding an element to a hashtable is O(n). So why do we say it's O(1)? because not all the elements take worst-case! Indeed, only the elements where doubling occurs takes worst-case. Therefore, inserting n elements takes <code>n+summation(2^i where i=0 to lg(n-1))</code> which gives <code>n+2n = O(n)</code> so that <code>O(n)/n = O(1)</code> !!! </p> <p>Why not apply the same principle to the linkedHashMap? You have to reload all the elements anyway! So, each time you are doubling <code>main</code>, put all the elements in <code>main</code> in mins as well, and sort them in <code>mins</code>. Then for all other cases proceed as above (bullets steps).</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