HackerRank Counting valley Solution Js

Hi, here is the solution for this HackerRank problem “Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly n steps. For every step he took, he noted if it was an uphillU, or a downhillD 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:

  • mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level.
  • valley is a sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level.

Given Gary’s sequence of up and down steps during his last hike, find and print the number of valleys he walked through.

For example, if Gary’s path is str = [DDUUUUDD] , he first enters a valley 2 units deep. Then he climbs out an up onto a mountain 2 units high. Finally, he returns to sea level and ends his hike.”

Function Description

Complete the countingValleys function in the editor below. It must return an integer that denotes the number of valleys Gary traversed.

countingValleys has the following parameter(s):

  • n: the number of steps Gary takes
  • str: a string describing his path

Explanation of the solution:

Gary have this path [D, D, U, U, U, D, D] (or like this format is the same value “DDUUUDD“) where a “D” means DOWN and “U” means UP.

Hi, I’m Gary! Let’s walk

Ok, then the first thing that we can do is draw a cartesian plane (I drew this in my notebook just for reference) if you see we have positive numbers at “Y” and negative numbers at “X” and also we have a middle line that is the “0” number and this “0will represent the Sea level 🙂 for Gary!

Gary have this path:

str = [D, D, U, U, U, D, D]

Nice!, if we take the first letter of the string this will be a “D” and because this means that the path is Down…well we draw a pink line below of the sea level ( “0” ) and we take the value where the line ended in this case will be -1.

-1

[D, D, U, U, U, D, D]

Then next char is a “D” also, so the same step we draw a pink line below the previous char and we take the value where that line ended in this case will be -2.

-2

[D, D, U, U, U, D, D]

Then next char is a “U” this mean that the path is Up, so we draw a blue line Up next to the previous char and we take the value where that line ended in this case will be -1.

-1

[D, D, U, U, U, D, D]

Then next char is also a “U“, so we draw a blue line Up above to the previous char and we take the value where that line ended in this case will be 0. CONGRATULATIONS GARY!!!! looks that you have your first Valley! at this point.

0

[D, D, U, U, U, D, D]

Then next char is also an “U” this mean that the path is Up, so we draw a blue line Up above to the previous char and we take the value where that line ended in this case will be 1.

1

[D, D, U, U, U, D, D]

Then next char is a “D“, so we draw a pink line next to the previous char and we take the value where that line ended in this case will be 0.

At this point we have a Mountain, but Gary is only interested in count the Valleys!

0

[D, D, U, U, U, D, D]

Then final char is a “D“, so we draw a pink line below to the previous char and we take the value where that line ended in this case will be -1.

-1

We done and the total of valleys of Gary path is 1.

Total of valley is 1

Nice, the last thing is write this solution into a code:

function countingValleys(n, str) {
    let valleyCounter = 0; // To count the valleys
    let altitude = 0; // To increment altitude in every "U" char, otherwise decrement the altitude

    // To go through every char in the string
    for (let i = 0;  i < n; i++) {
        let char = str.charAt(i); // To get a char in every cycle

        // Because we are just interested "U" char
        if (char === "U") { // check if the current char in the cycle is equal to "U"

            altitude++; // Increment the altitude if the char is "U"

            if (altitude === 0) { // This is the "key" when altitude is equal to 0 means that 1 valley was completed
                valleyCounter++; // Means that a valley was completed an increment
            }

        } else { // If is other char that not is "U" 
            altitude --; // then the altitude will be decremented
        }
    }

    // Finally when for cycle ends, return the valleyCounter
    return valleyCounter;
}

console.log('Number of valleys Gary traversed -->', countingValleys(7, 'DDUUUDD'));

Result:

By Cristina Rojas.