Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is a better method to solve this prob?
    primarykey
    data
    text
    <p>Here is the problem i am trying to solve, </p> <blockquote> <p>You are given a table with 2 rows and N columns. Each cell has an integer in it. The score of such a table is denfined as follows: for each column, consider the sum of the two numbers in the column; the maximum of the N numbers so obtained is the score. For example, for the table</p> <p>7 1 6 2<br/> 1 2 3 4</p> <p>the score is max(7 + 1; 1 + 2; 6 + 3; 2 + 4) = 9. The first row of the table is fixed, and given as input. N possible ways to ll the second row are considered:</p> <p>1; 2; : : : ; N<br/> 2; 3; : : : ; N; 1<br/> 3; 4; : : : ; N; 1; 2<br/> |<br/> N; 1; : : : ; ; N 1<br/></p> <p>For instance, for the example above, we would consider each of the following as possibilities for the second row.</p> <p>1 2 3 4<br/> 2 3 4 1<br/> 3 4 1 2<br/> 4 1 2 3<br/></p> <p>Your task is to find the score for each of the above choices of the second row. In the example above, you would evaluate the following four tables,</p> <p>7 1 6 2<br/> 1 2 3 4<br/> 7 1 6 2<br/> 2 3 4 1<br/> 7 1 6 2<br/> 3 4 1 2<br/> 7 1 6 2<br/> 4 1 2 3<br/> and compute scores 9, 10, 10 and 11, respectively</p> <p>Test data: N &lt;= 200000<br/> Time Limit: 2 seconds</p> </blockquote> <p>Here is the obvious method:</p> <p>Maintain two arrays A,B, Do the following n times</p> <ul> <li>add every element A[i] to B[i] and keep a variable max which stores the maximum value so far.</li> <li>print max</li> <li>loop through array B[i] and increment all the elements by 1, if any element is equal to N, set it equal to 1.</li> </ul> <p>This method will have take O(n^2) time, the outer loop runs N times and there are two inner loops which run for N times each.</p> <p>To reduce the time taken, we can find the maximum element M in the first row(in a linear scan), and then remove A[i] and B[i] whenever A[i] + N &lt;= M + 1.<br/> Because they will never be max.</p> <p>But this method might perform better in the average case, the worst case time will still be O(N^2).</p> <p>To find max in constant time i also considered using a heap, each element of the heap has two attributes, their original value and the value to be added. But still it will require a linear time to increment the value-to-be-added for all the elements of the heap for each of the n cases.<br/> And so the time still remains O(N^2)</p> <p>I am not able to find a method that will solve this problem faster than N^2 time which will be too slow as the value of N can be very large.<br/> Any help would be greatly appreciated.</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.
 

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