본문 바로가기
알고리즘/[python] 백준 BOJ

[python] 백준 2810 컵홀더

by Alan_Kim 2023. 3. 17.
728x90
반응형

https://www.acmicpc.net/problem/2810

 

2810번: 컵홀더

첫째 줄에 좌석의 수 N이 주어진다. (1 ≤ N ≤ 50) 둘째 줄에는 좌석의 정보가 주어진다.

www.acmicpc.net

 

문제 해결

 컵홀더와 사람과의 일대일 대응을 만들면 된다.

 stack을 이용해 남는 컵홀더와 컵홀더가 없는 사람을 저장해서 대응을 맞추면 될 것이다.

 대응을 못맞추는 사람을 hurdle이라는 변수를 통해 인원수를 저장한다.

 n에서 hurdle을 빼면 끝

 

CODE

import sys
input = sys.stdin.readline
n = int(input())
seats = list(input().rstrip())
stack = [0]
hurdle = 0
while seats:
    x = seats.pop(0)
    if x == 'S':
        if len(stack)==0: continue
        if stack[-1] == 'L':
            stack.pop()
            continue
    elif x == 'L':
        if len(stack) !=0 and stack[-1] == 0:
            stack.pop()
            stack.append(seats.pop(0))
        elif len(stack)!=0 and stack[-1] == 'L':
            hurdle += 1 # 컵홀더 못얻는 사람
            seats.pop(0) #나머지 커플석 하나 빼기
        else:
            hurdle += 1
            stack.append(seats.pop(0))
if len(stack) != 0 and stack[-1] == 'L':
    stack.pop()
print(n-hurdle)

 

728x90
반응형

'알고리즘 > [python] 백준 BOJ' 카테고리의 다른 글

[python] 백준 9625 BABBA  (0) 2023.03.17
[python] 백준 10775 공항  (0) 2023.03.17
[python] 백준 2212 센서  (0) 2023.03.17
[python] 백준 9506 약수들의 합  (0) 2023.03.16
[python] 백준 10825 국영수  (0) 2023.03.16

댓글