Skip to content

Instantly share code, notes, and snippets.

@f3ath
Last active December 3, 2016 17:26
Show Gist options
  • Save f3ath/e0e53559329f03bcdce8b563831db37b to your computer and use it in GitHub Desktop.
Save f3ath/e0e53559329f03bcdce8b563831db37b to your computer and use it in GitHub Desktop.
<?php
function three_asc(array $a) {
if (count($a) < 3) {
return false;
}
$min = $max = $new_min = 0;
for ($i = 1; $i < count($a); $i++) {
if ($min < $max) {
if ($a[$max] < $a[$i]) {
return [$min, $max, $i];
} elseif ($a[$min] < $a[$i]) {
$max = $i;
} elseif ($a[$new_min] < $a[$i]) {
$min = $new_min;
$max = $i;
} else {
$new_min = $i;
}
} else {
if ($a[$max] < $a[$i]) {
$max = $i;
} elseif ($a[$i] < $a[$min]) {
$min = $max = $i;
}
}
}
return false;
}
$test = [
[
[1, 2, 3],
[0, 1, 2],
],
[
[4, 5, 3, 6, 4, 8, 10, 1],
[0, 1, 3],
],
[
[9, 5, 3, 6, 4, 8, 10, 1],
[2, 4, 5],
],
[
[1, 1, 2, 1, 3],
[0, 2, 4],
],
[
[1, 2, 1, 2, 3],
[0, 3, 4],
],
[
[],
false,
],
[
[2, 3, 2, 2, 3, 2, 3, 2, 3, 3, 2, 1],
false,
],
[
[6, 5, 4, 3, 2],
false,
],
[
[1, 2, 1, 2, 1, 0, 1],
false,
],
];
foreach ($test as $a) {
echo '[' . implode(', ', $a[0]) . '] => ';
$res = three_asc($a[0]);
if ($a[1] == $res) {
echo 'OK';
} else {
echo ' FAIL [' . implode(', ', $res) . ']';
};
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment