-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathE0015.cpp
More file actions
38 lines (34 loc) · 671 Bytes
/
E0015.cpp
File metadata and controls
38 lines (34 loc) · 671 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Problem Statement: https://www.hackerrank.com/challenges/counting-valleys/problem
*/
#include <iostream>
#include <string>
using namespace std;
int countingValleys(int n, string s) {
int count,totalValleys;
bool valleyStarted = false;
count = totalValleys = 0;
for(int i=0 ; i<s.length() ; i++){
if(s[i] == 'U')
count++;
else
count--;
if(!valleyStarted && count < 0)
totalValleys++;
if(!valleyStarted && count<0)
valleyStarted = true;
else if(valleyStarted && count>=0)
valleyStarted = false;
}
return totalValleys;
}
int main()
{
int n;
string s;
cin>>n;
cin.ignore();
getline(cin,s);
cout<<countingValleys(n,s);
return 0;
}