Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So the OP's problem in general can be solved by the DP algorithm given in <a href="https://stackoverflow.com/questions/6454598/stuck-with-an-interview-question-partitioning-of-an-array">this question</a>. I'll repeat the crux of it here for completeness:</p> <blockquote> <p>Suppose <em>d[i][j]</em> is solution of the problem when we have items <em>s[1], .., s[i]</em> and <em>j</em> containers. Then:</p> <ol> <li><em>d[0][j] = 0</em> for each <em>j</em></li> <li><em>d[i][1] = Sum(s[1], ..., s[i])</em> for each <em>i</em></li> <li><em>d[i][j] = min(max(d[i-t][j-1], Sum(s[i-t+1], ..., s[i]) over all 1&lt;=t&lt;=i)</em></li> </ol> </blockquote> <p>However, the naive implementation consumes O(NK) space, which is too much. Fortunately, look at (3): <code>d[_][j]</code>'s value depends only on <code>d[_][j-1]</code>. This means that once we're done computing all <code>d[_][j]</code>, we can use the space we were using to store <code>d[_][j-1]</code> to instead store the values of <code>d[_][j+1]</code> we're about to compute.</p> <p>Since the values <code>d[0][j]</code> are all zero, we don't need to store those either. Hence the only arrays we need to store are the O(N)-sized <code>d[i][1]</code>, the O(N)-sized array that holds the result from the <code>j-1</code>th iteration, and the O(N)-sized array that holds the results from the <code>j</code>th iteration we're currently computing. Total: O(N).</p> <p>Edit: So first time round, I didn't actually answer the OP's question about how to count the number of containers at max weight. Suppose <code>w[i][j]</code> holds the max weight of the <code>i,j</code>th problem, and <code>c[i][j]</code> is the number of containers which match that max weight. Then:</p> <ol> <li><code>c[0][j] = j</code> and <code>w[0][j] = 0</code> for each <code>j</code></li> <li><code>c[i][1] = 1</code> and <code>w[i][1] = Sum(s[1], .., s[i])</code> for each <code>i</code></li> <li>Let <code>u</code> be equal to the <code>t</code> chosen in pt (3) above. <ul> <li>If <code>d[i-u][j-1] &gt; Sum(s[i-u+1], .., s[i])</code>: <ul> <li>then <code>c[i][j] = c[i-u][j-1]</code> and <code>w[i][j] = w[i-u][j-1]</code></li> <li>because the <code>j</code>th container with weight <code>Sum(s[i-u+1], .., s[i]</code> is lighter than the heaviest container out of containers <code>1, .., j-1</code>, which has weight <code>d[i-u][j-1]</code>. </li> </ul></li> <li>If <code>d[i-u][j-1] &lt; Sum(s[i-u+1], .., s[i])</code>: <ul> <li>then <code>c[i][j] = 1</code> and <code>w[i][j] = Sum(s[i-u+1], .., s[i])</code> </li> <li>because the <code>j</code>th container with items <code>s[i-u+1], .., s[i]</code> is the new heaviest container.</li> </ul></li> <li>If If <code>d[i-u][j-1] == Sum(s[i-u+1], .., s[i])</code>: <ul> <li>then <code>c[i][j] = c[i-u][j-1]+1</code> and <code>w[i][j] = d[i-u][j-1]</code></li> <li>because the <code>j</code>th container is the same weight as the previous heaviest container.</li> </ul></li> </ul></li> </ol> <p>Again, we only need to use a constant number of <code>O(N)</code> sized arrays, for <code>O(N)</code> time overall.</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.
 

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