Trail Valleys
A hiker follows a trail where each step changes their height relative to sea level. The function counting_valleys receives the number of steps steps and a string path, where U means a step up and D means a step down. A valley starts when the hiker goes below level 0 and ends when they return to sea level. Return the number of valleys walked through.
Примеры
Ввод
8, "UDDDUDUU"Вывод
1Ввод
12, "DDUUDDUDUUUD"Вывод
2Ввод
2, "UD"Вывод
0Нужно реализовать
def counting_valleys(steps: int, path: str) -> int:
Теги
stringsstack
Melde dich an, um den Einreichungsverlauf zu sehen
AnmeldenMelde dich an, um den KI-Mentor zu nutzen
AnmeldenVisible tests
1
Input
8, "UDDDUDUU"Expected
12
Input
12, "DDUUDDUDUUUD"Expected
23
Input
2, "UD"Expected
04
Input
2, "DU"Expected
15
Input
4, "DDUU"Expected
16
Input
1, "abc"Expected
0Focus radio
Paused · SomaFM · Fluid