해커랭크(HackerRank)

Counting Valleys

cepiloth 2018. 8. 19. 17:17
반응형


1. 문제


2. 알고리즘

키워드 - 구현


3. 코드


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
#include <bits/stdc++.h>
 
using namespace std;
 
int countingValleys(int n, string s) {
    // Complete this function
    
    // 초기 위치는 0
    int position = 0;
    // 깊이 
    int depth = 0;
    
    for(int i =0; i<n; i++) {
        if(s[i] == 'D') {
            position = position - 1;
            //cout << "D " << position << endl;
        } else {
            position = position + 1;
            //cout << "U " << position << endl;
        }
        
        if(== position && s[i] == 'U') {
            depth++;
        }
    }
    
    return depth;
}
 
int main() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int result = countingValleys(n, s);
    cout << result << endl;
    return 0;
}
cs

반응형