본문 바로가기
1 Day 1 Algorithms

[2018.12.13] Counting Valleys

by 곰돌찌 2018. 12. 14.

Problem

Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly  steps. For every step he took, he noted if it was an uphill, or a downhill step. Gary's hikes start and end at sea level and each step up or down represents a  unit change in altitude. We define the following terms:

  • 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.
  • 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 Gary's sequence of up and down steps during his last hike, find and print the number of valleys he walked through.

For example, if Gary's path is , he first enters a valley  units deep. Then he climbs out an up onto a mountain  units high. Finally, he returns to sea level and ends his hike.

Function Description

Complete the countingValleys function in the editor below. It must return an integer that denotes the number of valleys Gary traversed.

countingValleys has the following parameter(s):

  • n: the number of steps Gary takes
  • s: a string describing his path

Input Format

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

Constraints

Output Format

Print a single integer that denotes the number of valleys Gary walked through during his hike.

Sample Input

8
UDDDUDUU

Sample Output

1

Explanation

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

_/\               _
       \         /
          \/\/

He enters and leaves one valley.


How I solved the problem


# 얘는 이해하기가 좀 어려웠던 부분이 있음

# _ 는 sea level, / 는 up(+1), \는 down(-1)으로 보아야함

# sea level보다 높고 up and down이 있으면 얘는 mountain(산)

# sea level보다 낮고 up and down이 있으면 얘는 valley(계곡) 

# sea level이 0이 되는 시점에서 valley나 mountain이 하나로 치게됨.

# sea level(초기 0)이 up일때 +1, down일 때 -1로 설정하여 더하고 빼지면서 0이되는 시점을 찾아야함.

# 이전값과 비교하여서 이전 값이 음수이고 count가 0이 될때가 바로 계곡이 하나가 나타나는 시점이므로 이 때에 valley Count가 1 증가해야함.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
 
public class Solution {
 
    // Complete the countingValleys function below.
    static int countingValleys(int n, String s) {
        int sealevel = 0;
        int result = 0;
 
        for(int i = 0; i < n; i ++) {
            int compSealevel = sealevel;
            char comp = s.charAt(i);
            
            if (comp == 'U') {
                sealevel ++;
            } else if (comp == 'D') {
                sealevel --;
            }
 
            if (compSealevel < 0 && sealevel == 0) {
                result ++;
            }
        }
 
        return result;
    }
 
    private static final Scanner scanner = new Scanner(System.in);
 
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
 
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        String s = scanner.nextLine();
 
        int result = countingValleys(n, s);
 
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
 
        bufferedWriter.close();
 
        scanner.close();
    }
}
 
cs


[출처 : https://www.hackerrank.com ]

'1 Day 1 Algorithms' 카테고리의 다른 글

[2018.12.15] Class vs. Instance  (0) 2018.12.17
[2018.12.14] A Very Big Sum  (0) 2018.12.14
[2018.12.13] Compare the Triplets  (0) 2018.12.14
[2018.12.12] Sock Merchant  (0) 2018.12.14
[2018.12.12] Simple Array Sum  (0) 2018.12.14

댓글