Lesson 5 – Stacks and Queues – Fish demo task solution code written in php. Gives 100/100 score at the time of publishing. The description of the problem is copyrighted, so please see the following link for it: https://codility.com/demo/take-sample-test/fish
Solution in php:
function solution($A, $B) { $alive_count = 0; $downstream = array(); $downstream_count = 0; $cnt = count($A); for($i = 0; $i < $cnt; $i++){ if($B[$i] == 1){ $downstream[] = $A[$i]; $downstream_count++; } else{ while($downstream_count != 0){ if($downstream[count($downstream) - 1] < $A[$i]){ $downstream_count--; array_pop($downstream); } else{ break; } } if($downstream_count == 0){ $alive_count++; } } } $alive_count += count($downstream); return $alive_count; }
Given “AS IS”, can be ported from other languages from solutions found on the internet, please use with care.
Please note: we think that codility.com does not give a correct assessment of your real-world programming skills. For instance, reading the below would bring more understanding why: http://codility-test-questions.blogspot.com/2013/01/my-experience-with-codility-test.html