-
Notifications
You must be signed in to change notification settings - Fork 0
/
countValleys.cpp
63 lines (44 loc) · 1.2 KB
/
countValleys.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;
// Complete the countingValleys function below.
int countingValleys(int n, string s) {
// keep track of how far above sea level we are
int pos = 0;
// keep track of valleys
int numvalleys = 0;
// iterate thru the string
for(int i =0; i<n; i++){
// now figure out whether in valley or not
if(pos==0 && s[i]=='D'){
// at sea, going down, starting valley
numvalleys++;
}
if(pos==-1 && s[i]=='U'){
// below sea, going up, so ending valley
numvalleys++;
}
// calculate our relative position
if(s[i] == 'U'){
pos++;
}
else if(s[i] =='D'){
pos--;
}
}
// numvalleys should be 2* how many valleys we're at, so then divide by two
numvalleys /= 2;
return numvalleys;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string s;
getline(cin, s);
int result = countingValleys(n, s);
fout << result << "\n";
fout.close();
return 0;
}