πŸ”₯ 자료ꡬ쑰


덱 / 10866 / 싀버 4

문제 링크

import sys
from collections import deque

N = int(input())
a = deque([])

for i in range(N):
    S = sys.stdin.readline().split()
    if S[0] == "push_front":
        a.appendleft(int(S[1]))

    elif S[0] == "push_back":
        a.append(int(S[1]))

    elif S[0] == "pop_front":
        if len(a) != 0:
            print(a[0])
            a.popleft()
        else:
            print(-1)

    elif S[0] == "pop_back":
        if len(a) != 0:
            print(a[len(a)-1])
            a.pop()
        else:
            print(-1)

    elif S[0] == "size":
        print(len(a))

    elif S[0] == "empty":
        if len(a) == 0:
            print(1)
        else:
            print(0)

    elif S[0] == "front":
        if len(a) != 0:
            print(a[0])
        else:
            print(-1)

    elif S[0] == "back":
        if len(a) != 0:
            print(a[len(a)-1])
        else:
            print(-1)

정리

sys.stdin.readline()

Untitled

deque : double-ended queue

Untitled


μŠ€νƒ μˆ˜μ—΄ / 1874 / 싀버 3

문제 링크

μ½”λ“œ

n = int(input())
ans = []
S = []
num = 0
cnt = 1
# λˆ„μ  plusλ₯Ό ν•œ 횟수
# 더 큰 값이 λ‚˜μ™”μ„ λ•Œ ν•„μš”ν•œ + 횟수λ₯Ό 계산할 λ•Œ μ‚¬μš©

for i in range(n):
    num = int(input())

    # 더 큰 값이 λ‚˜μ™”μ„ λ•Œ push
    while cnt <= num:
        S.append(cnt)
        cnt += 1
        ans.append('+')

    # 더 μž‘μ€ 값이 λ‚˜μ™”μ„ λ•Œ pop
    # ν˜Ήμ€ pushλ₯Ό ν•œ 이후 pop ν•œ 번 μ‹œν–‰
    if S[-1] == num:
        S.pop()
        ans.append('-')

    # λΆˆκ°€λŠ₯
    else:
        print("NO")
        exit()

for i in ans:
    print(i)

정리