Php memory exceed limit

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;

Upvotes: 1

Views: 236

Answers (1)

Samir B
Samir B

Reputation: 152

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?

Upvotes: 1

Related Questions