I'm posting about my solutions to the HackerRank challenges as a way to improve my learning, reinforce my knowledge, and establish an understanding of the concepts covered. If I help someone by sharing along the way, even better!
Counting Valleys is one of the initial challenges with 'EASY' difficulty.
Gary is an avid climber. It tracks your walks meticulously, paying close attention to small details like topography. During his last walk he took exactly n
steps. For each step he took, he noted whether it was an up, U
, or a down, D
step. Gary's hikes start and end at sea level and each step up or down represents a 1 unit change in altitude. We define the following terms:
Given Gary's sequence of going up and down the steps during his last walk, find and print the number of valleys he walked. For example, if Gary's path is s = [DDUUUUDD]
, he will first enter a valley that is 2 units deep. Then he climbs a mountain 2 units high. Finally, he returns to sea level and finishes his walk.
A function that should return an integer with the number of valleys traveled by Gary.
8
UDDDUDUU
1
function countingValleys (n , s ) {
let level = 0 ,
valleys = 0 ,
i = 0 ;
for (i in s ) {
s .charAt (i) === 'U' ? level++ : level-- ;
level === 0 && s .charAt (i) === 'U' ? valleys++ : 0 ;
}
return valleys;
}
let level = 0 ,
valleys = 0 ,
i = 0 ;
First I created three variables; level
to track altitude; valleys
to keep count of the number of valleys Gary has traversed; i
to iterate the forloop;
I've created a for
loop that will go through the sequence of steps that Gary went through. for (i in s) {
The ternary condition if
checks if Gary's step was up U
: s.charAt(i) === 'U' ?
if was, increment the count: level++
If not, decrement the count. level--
I then check that Gary is at sea level level === 0
and that the step was up && s.charAt(i) === 'U'
, in this case representing that he has just climbed a valley. valleys++
As at the beginning of the challenge we are informed that Gary starts and ends his walks at sea level, we can ignore knowing if he climbed a mountain and focus only on the valley.
Finally, I finalize the function by returning the final count.
Other languages that I speak
def countingValleys (n , s )
level = 0
valleys = 0
s .each { | i |
i == 'U' ? level += 1 : level -= 1
level == 0 && i == 'U' ? valleys += 1 : 0
}
valleys
end
def countingValleys (n , s ):
level = 0
valleys = 0
for i in range(n ):
if (s [i] == 'U' ):
level += 1
else :
level -= 1
if (level == 0 and s [i] == 'U' ):
valleys += 1
return valleys
func countingValleys (n int32 , s string ) int32 {
var level, valleys int32
for _, i := range s {
if i == 'U' {
level++
} else {
level--
}
if level == 0 && i == 'U' {
valleys++
}
}
return valleys
}
def countingValleys (n : Int , s : String ): Int = {
var level = 0
var valleys = 0
s .foreach (el => {
if (el == 'U' ) {
level += 1
} else {
level -= 1
}
if (level == 0 && el == 'U' ) {
valleys += 1
}
})
return valleys;
}
static int countingValleys (int n , String s ) {
int level = 0 ,
valleys = 0 ;
for (int i = 0 ; i < n ; i++ ) {
if (s .charAt (i) == 'U' )
level++ ;
else
level-- ;
if (level == 0 && s .charAt (i) == 'U' )
valleys++ ;
}
return valleys;
}
function countingValleys ($n , $s ) {
$level = 0 ;
$count = 0 ;
for ($i = 0 ; $i < $n ; $i++ ) {
$s [$i] == 'U' ? $level++ : $level-- ;
$level == 0 && $s [$i] == 'U' ? $valleys++ : 0 ;
}
return $valleys;
}
Thought: Nurture your mind with great thoughts, for you will never go any higher than you think.