I have the following problem:
You are given N counters, initially set to 0, and you have two possible operations on them:
Here is an implementation in PHP:
function solution($N, $A) {
$output = array_fill(0, $N, 0);
$maxCounter = 0;
$minCounter = 0;
foreach ($A as $number) {
if($number === $N + 1) {
$minCounter = $maxCounter;
} else if($number <= $N) {
$number--;
if($minCounter > $output[$number]) {
$output[$number] = $minCounter;
}
$output[$number]++;
if($output[$number] > $maxCounter) $maxCounter = $output[$number];
}
}
foreach ($output as $index => $number) {
if($number < $minCounter) $output[$index] = $minCounter;
}
// var_dump($output);
return $output;
}