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

題目連結:Counting Valleys | HackerRank

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

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;
}

發佈留言