Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think the solution is pretty easy.</p> <p>Start with an array <code>x</code> initialised to <code>empty</code> values such that there is one space for each item you need to place.</p> <p>Then, for each <code>(item, frequency)</code> pair in descending order of frequency, assign <code>item</code> values to <code>x</code> in alternating slots starting from the first <code>empty</code> slot.</p> <p>Here's how it works for your example:</p> <pre><code>20*A A_A_A_A_A_A_A_A_A_A_A_A_A_A_A_A_A_A_A_A 10*B ABABABABABABABABABABA_A_A_A_A_A_A_A_A_A 5*C ABABABABABABABABABABACACACACACA_A_A_A_A 2*E ABABABABABABABABABABACACACACACAEAEA_A_A 1*F ABABABABABABABABABABACACACACACAEAEAFA_A </code></pre> <p>At this point we fail, since <code>x</code> still has an empty slot. Note that we could have identified this right from the start since we need at least 19 slots between the <code>A</code>s, but we only have 18 other items.</p> <p><strong>UPDATE</strong></p> <p>Leonidas has now explained that the items should be distributed "evenly" (that is, if we have k items of a particular kind, and n slots to fill, each "bucket" of n/k slots must contain one item of that kind.</p> <p>We can adapt to this constraint by spreading out our allocations rather than simply going for alternating slots. In this case (and let's assume 2 Fs so we can solve this), we would have</p> <pre><code>20*A A_A_A_A_A_A_A_A_A_A_A_A_A_A_A_A_A_A_A_A 10*B ABA_ABA_ABA_ABA_ABA_ABA_ABA_ABA_ABA_ABA 5*C ABACABA_ABACABA_ABACABA_ABACABA_ABACABA 2*E ABACABAEABACABA_ABACABAEABACABA_ABACABA 2*F ABACABAEABACABAFABACABAEABACABAFABACABA </code></pre>
 

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