A
A
Almaz_Ilyasov2014-03-26 14:42:35
PHP
Almaz_Ilyasov, 2014-03-26 14:42:35

How to simplify the algorithm for filling a one-dimensional array?

Hello, please help me to solve one problem in a simpler way, maybe I missed something.
There is an empty one-dimensional array.
When filling array cell (a), it is necessary to make sure that out of n cells closest to it, no more than m are filled .
The most banal thing is to go through the cycle:

$result = true;
for($i=$a-$n;$i<=$a+n;$i++)
{
 $c = 0;
 for($j=$i;$j<=$i+n;$j++)
 {
  if($arr[$j]==true)
  {
   $c++;
  }
 }
 if($c>$m)
 {
  $result = false;
  return $result;
 }
}

Any ideas how to simplify the algorithm?
Maybe, in general, php solved the problem in advance, and there is a ready-made function?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vampiro, 2014-03-26
@Vampiro

You are right, this task has indeed been simplified for a long time: they
store the schedule in the database, and find out the number of classes with a query like

select count(id) as 'ok' from tableName where `owner`=:ownerId and (`date` between :lowDate and :hiDate)

S
Snewer, 2014-03-26
@Snewer

Your code is productive enough

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question