You are viewing a single comment's thread. Return to all comments →
C++:
int countingValleys(int steps, string path) { int count_ups = 0; int count_downs = 0; int total_valleys = 0; bool is_valley= false; for(int i = 0; i < steps; i++) { (path[i] == 'D') ? ++count_downs : ++count_ups; int level = count_ups - count_downs; if(level < 0) { is_valley = true; } else if(is_valley && level == 0) { total_valleys++; is_valley = false; } } return total_valleys; }
Seems like cookies are disabled on this browser, please enable them to open this website
Counting Valleys
You are viewing a single comment's thread. Return to all comments →
C++: