Federico Pintaluba
I'm a 27 years old developer, I have been coding for more than a decade, but I have focused in Android Development for the past 5 years. Here you will find all my projects, ideas, thoughts. Basically everything I made with <3.

HackerRank: Counting Valleys

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 uphill U , 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 8 = [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
  • s: a string describing his path

Input Format

The first line contains an integer n, the number of steps in Gary’s hike.
The second line contains a single string 8, of n characters that describe his path.

Constraints

Output Format

Print a single integer that denotes the number of valleys Gary walked through during his hike.

Sample Input

8
UDDDUDUU

Sample Output

1

Explanation

If we represent _ as sea level, a step up as /, and a step down as \, Gary’s hike can be drawn as:

_/\      _
   \    /
    \/\/

He enters and leaves one valley.

Solution

Ok, first of all let’s understand the terms they are using in this problem:

  • 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.

In the explanation section we clearly see one valley, it’s everything that happens below sea level, starting with a step down from sea level, and ending with a step up TO sea level.

So, we want to know where we are on each step, and the amount of valleys we went through, let’s define those variables first.

var valleyCount = 0
var currentLevel = 0

Let’s think about this for a moment, first of all, we start at the sea level, that makes us start at currentLevel 0, so going straight to D, will start a valley, but if the List starts with U, we need to know how many steps are left to get below sea level, that’s easy, let’s add/subtract 1 depending if the next step in the List is U or D respectively.

    s.forEach { step ->
        if (step == 'U')
            currentLevel++
        else
            currentLevel--
    }

The inline function forEach already facilitates us a quick way to iterate over each Int in the list of steps, for each steps, we look for U or D (else), we know this are the only two possible values.

Now, we need to know when we get into a valley, that will happen when our currentLevel goes below zero, but also, we need to ensure to not count again when we are already in the valley, and we didn’t go over the sea level again. Let’s define a quick boolean to know that.

var inValley = false

In our iteration over the steps, let’s check, if we go below sea level and we are not in the valley, let’s sum up one valley to our valleyCount variable.

        if (!inValley && currentLevel < 0) {
            valleyCount++
            inValley = true
        }

We don’t really care what happens when we are already in the valley, so we need to wait until we get to the sea level again in order to let our code know it’s allowed to start another valley.

So let’s set inValley to false again, when we go above 0 in our currentLevel variable.

        if (currentLevel >= 0)
            inValley = false

Now, outside our forEach (of course), let’s return our valleyCount variable.

And, with this we are hopefully done, let’s first check running this code with sample input on our own environment.

8
UDDDUDUU

Running…

1

Let’s submit the code in HackerRank and see if we pass all test cases.

Hope this helped you understand a fair solution made in Kotlin for this problem, if you have any suggestion or a better way of doing this in the same language, don’t hesistate on commenting below!

Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *