Computer Science/Coding Test

[HackerRank] Counting Valleys

Nomad Lee 2025. 4. 15. 16:19

https://www.hackerrank.com/challenges/counting-valleys/problem

 

Counting Valleys | HackerRank

Count the valleys encountered during vacation.

www.hackerrank.com

[문제 ]

An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly  steps, for every step it was noted if it was an uphill, U , or a downhill, D step. Hikes always start and end at sea level, and each step up or down represents a 1 unit change in altitude. We define the following terms:

  • A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level.
  • A valley is a sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level.

Given the sequence of up and down steps during a hike, find and print the number of valleys walked through.

자신의 하이킹을 꼼꼼하게 기록하는 등산객이 있습니다. 마지막 하이킹에서는 모든 걸음마다 정확히 몇 걸음씩 걸었는지 오르막인지(U) 내리막인지(D) 표시했습니다. 하이킹은 항상 해수면에서 시작하고 끝나며, 각 오르막 또는 내리막은 고도의 1 단위 변화를 나타냅니다. 다음과 같은 규칙을 정의합니다.

  • 산은 해수면 위로 올라가는 걸음의 연속으로, 해수면에서 올라가는 걸음으로 시작하여 해수면으로 내려가는 걸음으로 끝납니다.
  • 계곡은 해수면 아래로 내려가는 걸음의 연속으로, 해수면에서 내려가는 걸음으로 시작하여 해수면으로 올라가는 걸음으로 끝납니다.

하이킹 중 오르막과 내리막 걸음의 순서가 주어졌을 때, 지나간 계곡의 개수를 구하여 출력합니다.

 

Example

 steps = 8path = [DDUUUUDD]

The hiker first enters a valley 2 units deep. Then they climb out and up onto a mountain 2 units high. Finally, the hiker returns to sea level and ends the hike.

등산객은 먼저 2단위2 단위 깊이의 계곡에 들어갑니다. 그런 다음 다시 나와 2 단위 높이의 산으로 올라갑니다. 마지막으로 해수면으로 돌아와 하이킹을 마칩니다.

Function Description

Complete the countingValleys function in the editor below.

countingValleys has the following parameter(s):

  • int steps: the number of steps on the hike
  • string path: a string describing the path

countingValleys 함수를 완성하세요.

countingValleys에는 다음 매개변수가 있습니다.

  • int steps: 하이킹의 걸음 수
  • string path: 경로를 설명하는 문자열

Returns

  • int: the number of valleys traversed

int: 통과한 계곡의 수

Input Format

The first line contains an integer steps, the number of steps in the hike.
The second line contains a single string path, of steps characters that describe the path.

첫 번째 줄에는 하이킹 코스의 걸음 수를 나타내는 정수 steps가 주어집니다.
두 번째 줄에는 경로를 설명하는 문자로 구성된 단일 문자열 path가 주어집니다.

Sample Input

8
UDDDUDUU

Sample Output

1

Explanation

If we represent _ as sea level, a step up as /, and a step down as \, the hike can be drawn as:

_를 해수면으로, 위로 올라가는 단계를 /로, 아래로 내려가는 단계를 \로 나타내면 상승은 다음과 같이 나타낼 수 있습니다.

_/\      _
   \    /
    \/\/

The hiker enters and leaves one valley.

등산객은 계곡하나를 들어갔다 나왔습니다.

[python 코드]

def countingValleys(steps, path):
    result = 0
    alt = 0
    for p in path:
        if p == 'U':
            alt += 1 
            if alt == 0:
                result += 1
        else:
            alt -= 1
    return result

계곡은 해수면 아래로 내려가는 걸음으로 시작하여 해수면으로 올라가는 걸음으로 끝나기 때문에 U가 다시 해수면 0으로 돌아오는 시점만 체크

 

'Computer Science > Coding Test' 카테고리의 다른 글

[HackerRank] The Minion Game  (1) 2025.04.21
[HackerRank]Repeated String  (0) 2025.04.19
[HackerRank] Jumping on the Clouds  (0) 2025.04.16
[HackerRank] Sales by Match  (0) 2025.04.15