Created
November 28, 2018 17:48
-
-
Save doganoo/f2d7bc6533313b665c97db12ff5c97d8 to your computer and use it in GitHub Desktop.
second highest value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//assumes that you have installed PHPAlgorithms properly: | |
//https://github.com/doganoo/PHPAlgorithms | |
$arr = [1, 2, 3, 4, 5, 6, 7, 8]; | |
echo secondHighest($arr); //echo's 7 | |
function secondHighest(array $array): int { | |
$maxHeap = new MaxHeap(); | |
foreach ($array as $value) { | |
$maxHeap->insert($value); | |
} | |
print_r($maxHeap->getHeap()); | |
return $maxHeap->getHeap()[1] > $maxHeap->getHeap()[2] ? $maxHeap->getHeap()[1] : $maxHeap->getHeap()[2]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment