0%

HackerRank Warm-up 2.Counting Valleys , in C++

題目連結:Counting Valleys | HackerRank

唯一要注意的是山谷的定義

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int countingValleys(int steps, string path) {
int ct=0,h=0;
bool islow=false;
for(auto a:path){
if(a=='D'){
h--;
}else{
h++;
}
if(h<0){
if(!islow){
ct++;
islow=true;
}
}else{
islow=false;
}
}
return ct;
}