Counting Valleys
Given the sequence of up and down steps during a hike, find and print the number of valleys walked through.
ip:
8
UDDDUDUUop:1 _/\ _
\ /
\/\/
int countingValleys(int n, string s) {
int level=0;
int valley=0;
for(auto c: s)
{
if(c=='U')
{
if(level==-1)
{
valley++;
}
level++;
}
else{
level--;
}
}
return valley;
}
Comments
Post a Comment