**Counting Valleys**

### Problem Statement :

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: A 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. A 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 <= 10^6 path[i] belongs to {U, D}

### Solution :

` ````
Solution in C :
python 3 :
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);
}
}
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;
}
C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%i", &n);
char str[n];
scanf("%s", str);
int level = 0, result = 0, valley = 0;
for (int i = 0; i < n; i++) {
if(str[i] == 'U') {
level++;
if(level == 0 && valley) {
valley = 0;
result++;
}
}
else if(str[i] == 'D') {
if(level == 0)
valley = 1;
level--;
}
}
printf("%i", result);
return 0;
}
```

## View More Similar Problems

## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →