Created
April 15, 2020 14:07
-
-
Save MagedAhmad/a938fe4620e4fca99773482233e06b2e to your computer and use it in GitHub Desktop.
Hackerrank Counting Valleys challenge
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
| <?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