본문 바로가기

problem solving

[프로그래머스]쇠막대기

'('가 나오면 막대기가 하나씩 쌓인다.

'(' 다음에 ')'가 나오면 레이저가 발생하는데 이때 '('의 수 -1만큼(레이저 제외) 쇠막대기의 개수가 추가된다.

')' 가 연속적으로 나오면 쇠막대기의 끝을 의미하므로 +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
#include <string>
#include <vector>
 
using namespace std;
 
int solution(string arrangement) {
    int answer = 0;
    
    int count = 0;
    for (int i = 0; i < arrangement.size(); i++) {
        if (arrangement[i] == '(') {
            count++;
        }
        else if (arrangement[i] == ')') {
            count--;
            if (i >  0 && arrangement[i - 1== '(') {
                answer += count;
            }
            else {
                answer++;
            }
        }
    }
    return answer;
}
cs

'problem solving' 카테고리의 다른 글

[프로그래머스]프린터  (0) 2020.03.07
[프로그래머스]주식가격  (0) 2020.03.07
[프로그래머스]기능개발  (0) 2020.03.07
[프로그래머스]스킬트리  (0) 2020.03.07
[프로그래머스]탑  (0) 2020.03.07