Lesson 5 – Stacks and Queues – StoneWall (sigma2012) 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/stone_wall
Solution in php:
function solution($H) { $N = count($H); $stones = 0; $stack = array_fill(0, $N, 0); $stack_num = 0; for($i = 0; $i < $N; $i++){ while($stack_num > 0 && $stack[$stack_num - 1] > $H[$i]){ $stack_num--; } if($stack_num > 0 && $stack[$stack_num - 1] == $H[$i]){ continue; } else{ $stones++; $stack[$stack_num++] = $H[$i]; } } return $stones; }
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