My answer for this was like below
function solution($A) {
// write your code in PHP7.0
$res = 0;
$h = 0;
$i = 0;
foreach ($A as $a) {
if (isset($A[$i+1])) {
if ($a == $A[$i+1]) {
$res++;
continue;
}
if ($h >= 2) {
$res++;
$h = 0;
}
if ($a < $A[$i+1]) {
$h++;
}
if ($h <= -2) {
$res++;
$h = 0;
}
if ($a > $A[$i+1]) {
$h--;
}
}
$i++;
}
return $res;
}
function solution($A) { // write your code in PHP7.0
$res = 0;
$h = 0;
$i = 0;
foreach ($A as $a) {
if (isset($A[$i+1])) {
if ($a == $A[$i+1]) {
$res++;
continue;
}
if ($h >= 2) {
$res++;
$h = 0;
}
if ($a < $A[$i+1]) {
$h++;
}
if ($h <= -2) {
$res++;
$h = 0;
}
if ($a > $A[$i+1]) {
$h--;
}
}
$i++;
}
return $res;
}