1

Recently I join a programming contest, and on this one question. When I try to submit, in some testcase I get memory exceed limit. I does not have the testcase and also the question as the contest already ended. The question is something like this.

You been ask to help a company reduce the number of candidate for the job interview. If any of the candidate has the CGPA, number of experience and test score lest then any others candidate, he will be rejected. Find the number of rejected candidate


The input will be from command line. Something like this,
First line will have a single integer . is the number of candidate n .
Second line will have n integer seperate by space represent the CGPA for each candidate.
Third line will have n integer seperate by space represent the experience for each candidate.
Fourth line will have n integer seperate by space represent the testscore for each candidate.

Example Input
4
1 3 5 6
6 2 6 1
1 2 3 4

Example output
1

Because only candidate 2 has all 3 value less the anothers candidate which is candidate 3. Candidate 1 has number of experience equal to candidate 3, this is consider ok

Here is my code, just want to know if anyone know how to reduce the memory usage.

<?php
$input = fopen('php://stdin', 'r');
$numberOfPeople = intval(fgets($input));
$numberOfCGA = explode(' ', trim(preg_replace('/\s+/', ' ', fgets($input))));
foreach( $numberOfCGA as $key => $num )
{
    $numberOfCGA[$key] = intval($num);
}
$numberOfExp = explode(' ', trim(preg_replace('/\s+/', ' ',    fgets($input))));
foreach( $numberOfExp as $key => $num )
{
    $numberOfExp[$key] = intval($num);
}
$numberOfScore = explode(' ', trim(preg_replace('/\s+/', ' ', fgets($input))));
foreach( $numberOfScore as $key => $num )
{
    $numberOfScore[$key] = intval($num);
}
$numOfReject = 0;

for( $i = 0; $i < $numberOfPeople; $i++ )
{
    for( $j = 0; $j < $numberOfPeople; $j++ )
    {
        if( $i == $j ) continue;
        if( ( intval($numberOfCGA[$i]) < intval($numberOfCGA[$j]) )
            && ( intval($numberOfExp[$i]) < intval($numberOfExp[$j]) )
            && ( intval($numberOfScore[$i]) < intval($numberOfScore[$j]) ) )
        {
            $numOfReject++;
            break;
        }
    }
}
echo $numOfReject . PHP_EOL;
1
  • fgets can read the stream until the end, otherwise specify the second param. Commented Jun 13, 2015 at 16:35

1 Answer 1

1

what if you increase your php memory? See http://tutorials.hostucan.net/how-to-increase-php-memory-limit/ for more information.

Also check this out: PHP preg_replace() - Memory Issues. Alternative?

Sign up to request clarification or add additional context in comments.

1 Comment

Increase the memory limit is not an option as the contest allow only such memory to be use. But I will try to check if pref_replace is the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.