We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static int countingValleys(int steps, String path) {
int seaLevel = 0, valleys = 0, currentLevel = 0;
for (char step : path.toCharArray()) {
if (step == 'U') {
currentLevel++;
} else {
currentLevel--;
}
// If we just came up to sea level from below, we've exited a valley
if (currentLevel == 0 && step == 'U') {
valleys++;
}
}
return valleys;
}
Cookie support is required to access HackerRank
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 →