Skip to content

Instantly share code, notes, and snippets.

@MagedAhmad
Created April 15, 2020 14:07
Show Gist options
  • Select an option

  • Save MagedAhmad/a938fe4620e4fca99773482233e06b2e to your computer and use it in GitHub Desktop.

Select an option

Save MagedAhmad/a938fe4620e4fca99773482233e06b2e to your computer and use it in GitHub Desktop.
Hackerrank Counting Valleys challenge
<?php
function countingValleys($n, $s) {
$count = 0;
$track = 0;
for($i = 0; $i < $n; $i++) {
if($s[$i] == 'U') {
$track++;
}else {
if($track == 0) {
$count++;
$track--;
}else {
$track--;
}
}
}
return $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment