Counting Valleys | HackerRank Solution

Hello coders, today we are going to solve Counting Valleys HackerRank Solution which is a Part of HackerRank Algorithm Series.

Contents

An avid hiker keeps meticulous records of their hikes. During the last hike that took exactlyÂ stepsÂ steps, for every step it was noted if it was anÂ uphill,Â U, or aÂ downhill,Â DÂ step. Hikes always 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 the sequence of up and down steps during a hike, find and print the number of valleys walked through.

Example

steps = 8 path = [DDUUUUDD]
The hiker first enters a valleyÂ 2Â units deep. Then they climb out and up onto a mountainÂ 2Â units high. Finally, the hiker returns to sea level and ends the hike.

Function Description

Complete the countingValleys function in the editor below.

countingValleys has the following parameter(s):

• int steps: the number of steps on the hike
• string path: a string describing the path

Returns

• int:Â the number of valleys traversed

Input Format

The first line contains an integerÂ steps, the number of steps in the hike.
The second line contains a single stringÂ path, ofÂ stepsÂ characters that describe the path.

Constraints

• 2 <= steps <= 106
• path[i] belongs to {U D}

Sample Input

``````8
UDDDUDUU``````

Sample Output

``1``

Explanation

If we representÂ `_`Â as sea level, a step up asÂ `/`, and a step down asÂ `\`, the hike can be drawn as:

``````_/\      _
\    /
\/\/``````

The hiker enters and leaves one valley.

Solution – Counting Valleys

C++

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int l;
string str; cin>>l>>str;
int height = 0;
int count = 0;
for(int i=0;i<l;i++){
if (str[i]=='U') height++;
else {
if (height==0) count++;
height--;
}
}
if (height<0) count--;
cout<<count<<endl;
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
```

Python

```N=int(input())
S=input()
L=0
V=0
for s in S:
if s == 'U':
L += 1
if L == 0:
V += 1
else:
L -= 1
print(V)```

Java

```import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
in.nextInt();
String s = in.next();

int level = 0;
int valleys = 0;
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == 'U'){
level++;
}else if(s.charAt(i) == 'D'){
if(level == 0){
valleys++;
}
level--;
}
}
System.out.println(valleys);
}
}
```

Disclaimer: The above Problem (Counting Valleys) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.