Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think there is a solution with <code>O(1)</code> for all operations.</p> <p>You need a deque, and two hashtables. </p> <p>The first one is a linked hashtable, where for each <code>element</code> you store its <code>count</code>, the <code>next</code> element in count order and a <code>previous</code> element in count order. Then you can look the next and previous element's entries in that hashtable in a constant time. For this hashtable you also keep and update the element with the largest count. (<code>element -&gt; count, next_element, previous_element</code>)</p> <p>In the second hashtable for each distinct number of elements, you store the elements with that count in the start and in the end of the range in the first hashtable. Note that the size of this hashtable will be less than <code>n</code> (it's <code>O(sqrt(n))</code>, I think). (<code>count -&gt; (first_element, last_element)</code>)</p> <p>Basically, when you add an element to or remove an element from the deque, you can find its new position in the first hashtable by analyzing its <code>next</code> and <code>previous</code> elements, and the values for the old and new count in the second hashtable in constant time. You can remove and add elements in the first hashtable in constant time, using algorithms for linked lists. You can also update the second hashtable and the element with the maximum count in constant time as well.</p> <p>I'll try writing pseudocode if needed, but it seems to be quite complex with many special cases.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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