Coding, Problem Solving, Teaching

Problem Solving: Counting Valleys

This problem is extracted from HackerRank: Counting Valleys. Its rating is Easy. We will be using Python 3 to solve this problem. Do try to solve it yourself first in HackerRank before peeking at the solution. 🙂

The 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 or a downhill step. Gary’s hikes start and end at sea level. We define the following terms:

  • A mountain is a non-empty sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level.
  • A valley is a non-empty 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.

Input Format
The first line contains an integer, n, denoting the number of steps in Gary’s hike.

The second line contains a single string of n characters. Each character is ∈ {U, D} (where U indicates a step up and D indicates a step down), and the ith character in the string describes Gary’s ith step during the hike.

Constraints
2 ≤ N ≤ 106

Output Format
Print a single integer denoting 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:
Counting Valleys

It’s clear that there is only one valley there, so we print 1 on a new line.

Approaching the Problem

We shall define a function named count_valleys that will take in two arguments, n and steps, where n is the total number of steps Gary takes, and steps is a string object containing a string of U or D characters. This function will return the number of valleys.


First, think about what you need to keep track of. The most essential bit is, of course, the number of valleys, as we have to return this value.

Using the illustration in the Explanation portion in the question, we have to know the overall topography by using our program to know Gary’s position every step he takes. We can use an integer variable, level, to track how far above or below the sea level Gary is. This variable level will start at 0 to indicate Gary’s starting position at sea level. Whenever we encounter 'U' in Gary’s step, we will increase level by 1. When we encounter 'D', we will decrease level by 1. This means that when the value of level is positive, he is above sea level on a mountain, and when it is negative, he is in a valley.

Version 1 of our function:

def count_valleys(n, steps):
	num_valleys = 0    # number of valleys
	level = 0          # starts at sea level
	for d in steps:
		if d == 'U':   # going upslope
			level += 1
		else:          # going downslope
			level -= 1
	return num_valleys

At this point, we are keeping track of Gary’s position relative to sea level, but not the number of valleys yet. Next, we should think about when we can increment num_valleys.

Let’s break down the problem into more digestible parts:

  • At each loop, we analyse the direction Gary does: It can only be 'U' (upslope) or 'D' (downslope).
  • When it’s a 'U', there’s no reason for us to increment num_valleys, since Gary is climbing back up.
  • Now, when it’s a 'D', how can we consider it a new valley, so that we may increment num_valleys?

To answer the last point, for it to be considered a new valley, the level variable must be 0 in the previous loop (in other words, he must be at sea level before stepping downslope). This special consideration must be included in the else case of our function above (where d == 'D').

Hence, before we decrement level, we need to do a test if Gary was previously at sea level (level == 0). If so, then increment num_valleys.


Final version of our function:

def count_valleys(n, steps):
	num_valleys = 0        # number of valleys
	level = 0              # starts at sea level
	for d in steps:
		if d == 'U':       # going upslope
			level += 1
		else:              # going downslope
			if level == 0:
				num_valleys += 1
			level -= 1
	return num_valleys

Final Solution

With the above function defined, we can now call it in our final program:

def count_valleys(n, steps):
	num_valleys = 0        # number of valleys
	level = 0              # starts at sea level
	for d in steps:
		if d == 'U':       # going upslope
			level += 1
		else:              # going downslope
			if level == 0:
				num_valleys += 1
			level -= 1
	return num_valleys

n = int(input().strip())
steps = input().strip()
result = count_valleys(n, steps)
print(result)

1 thought on “Problem Solving: Counting Valleys

  1. Hi, there is a mistake in your approach,

    if we see the definition of valley “A valley is a non-empty sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level.”

    because of this, the only zeros that we should count, are the ones that exist when we add one, not when we take one. Because we are finishing in a step up, not a step down.

    The code how it currently is you are counting mountains, not valleys.

    this is the correction:
    ———————-
    for d in steps:
    if d == ‘U’: # going upslope
    level += 1
    if level == 0:
    num_valleys += 1
    else: # going downslope
    level -= 1
    return num_valleys
    ————————–

Leave a Reply

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