Home 스택 백준 10828번
Post
Cancel

스택 백준 10828번

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

문제를 설명할 것도 없고 그 자체 스택을 만들어 보는 것인데

아마 파이썬으로 하면 안풀릴거다

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
from collections import deque

stack_list = deque([])
loop = int(input())
for i in range(loop):
    string = list(input().split())
    if len(string)==1:
        order = str(string[0])
        if order == 'pop':
            if len(stack_list) != 0:
                print(stack_list.pop())
            else:
                print(-1)
        elif order == 'size':
            print(len(stack_list))
        elif order == 'empty':
            if len(stack_list)==0:
                print(1)
            else:
                print(0)
        elif order == 'top':
            if len(stack_list) != 0:
                print(stack_list[-1])
            else:
                print(-1)
    else:
        order = string[0]
        number = int(string[1])
        stack_list.append(number)

사실 큐가 아니라서 deque를 사용하지 않아도 된다. pop와 append 모두 left를 기준으로 하기 때문이다.

그래서 pop, size, empty, top, push 조건에 따라서 구현을 하고 해당하는 출력은 명령어가 입력될때 맞춰서 진행하면 되는데

result

문제는 시간 제한이 0.5초로 엄격하다보니 Pypy로 해도 시간초과가 걸린다.

…..

이거는 이대로 넘어갈려고 한다.

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.