Last active
October 12, 2015 16:58
-
-
Save connordavison/ed5051843dff7fcc623f to your computer and use it in GitHub Desktop.
Solution to Codility problem https://codility.com/programmers/task/falling_disks
This file contains 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
<?php | |
function solution($A, $B) { | |
// Build an array relating maximum circumferences to depths | |
$smallest_at = [-1 => PHP_INT_MAX]; | |
for ($i = 0; $i < count($A); $i++) { | |
$smallest_at[$i] = min($smallest_at[$i - 1], $A[$i]); | |
} | |
// Start dropping | |
$dropped = 0; | |
// Reverse our way through the $smallest_at array and attempt to drop | |
// where possible | |
for ($i = count($A) - 1; $i >= 0 && $dropped < count($B); $i--) { | |
if ($smallest_at[$i] >= $B[$dropped]) { | |
$dropped++; | |
} | |
} | |
return $dropped; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment