[백준] 10799: 쇠막대기 (Python)

JiKwang Jeong·2021년 10월 12일
0

문제📖

풀이🙏

  • (를 만나는 경우 stack에 더한다.
  • )를 만나는 경우 이전이 (이면 (즉, ()이면) stack pop 실시후 남아있는 스택의 원소 개수를 전체 결과에 더해준다.
  • ()아닌 경우 pop 이후 막대의 끄트머리 이므로 1을 더해준다.

코드💻

import sys
input = sys.stdin.readline
data = list(input().rstrip())
stack = []
result = 0

for i in range(len(data)):
    if data[i] == '(':
        stack.append('(')
    else:
        if data[i-1] == '(':
            stack.pop()
            result += len(stack)
        else:
            stack.pop()
            result += 1
print(result)
profile
기억보다 기록, 난리보다 정리

0개의 댓글