You are viewing a single comment's thread. Return to all comments →
C++ solution
int countingValleys(int steps, string path) { int seaLevel = 0; int valleyCount = 0; bool valleyStarted = false; bool valleyFinished = false; for(const auto& c : path ) { if(c == 'D') { seaLevel--; if(seaLevel == -1) { valleyStarted = true; } } else if(c == 'U') { seaLevel++; if(valleyStarted && seaLevel == 0) { valleyFinished = true; } } if(valleyFinished && valleyStarted) { valleyStarted = false; valleyFinished = false; valleyCount++; } } return valleyCount; }
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++ solution