Answer the question
In order to leave comments, you need to log in
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;
}
}
Answer the question
In order to leave comments, you need to log in
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)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question