You are viewing a single comment's thread. Return to all comments →
My Java solution:
public static int countingValleys(int steps, String path) { int currentLevel = 0; char down = 'D'; int numberOfValleys = 0;
for (Character c : path.toCharArray()) { int step = c == down ? -1 : 1; if (currentLevel < 0 && (currentLevel + step) == 0) { numberOfValleys++; } currentLevel += step; } return numberOfValleys; }
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 Java solution:
public static int countingValleys(int steps, String path) { int currentLevel = 0; char down = 'D'; int numberOfValleys = 0;