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;
int valleys = 0;
for (char step : path.toCharArray()) {
seaLevel += (step == 'U') ? 1 : -1;
// Detect if a valley has been exited
if (seaLevel == 0 && step == 'U') {
valleys++;
}
}
return valleys;
}
sd
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 →
My solutions in Java:
1:
2: more concise
sd