1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
from collections import deque
import sys
ans=deque()
n=int(input())
for i in range(n):
tmp=list(sys.stdin.readline().split())
if tmp[0]=='push' : ans.append(int(tmp[1]))
if tmp[0]=='top' :
if len(ans)==0: print(-1)
else: print(ans[-1])
if tmp[0]=='size': print(len(ans))
if tmp[0]=='empty':
if len(ans)==0 : print(1)
else :print (0)
if tmp[0]=='pop':
if len(ans)==0: print (-1)
else:
print(ans[-1])
ans.pop()
|
cs |
python에는 스택이 없죠
그래서 list 나 deque를 이용해서 구현해야하구요
여기서 특이사항은
입력은 sys.stdin으로 받으셔야 시간초과가 안뜹니더
반응형
'python-algorithm' 카테고리의 다른 글
백준 10866 덱 (0) | 2021.08.24 |
---|---|
백준 10845 큐 (0) | 2021.08.24 |
백준 2164 카드 2 (0) | 2021.08.24 |
백준 10814 나이순 정렬 (0) | 2021.08.23 |
백준 11650 좌표 정렬하기 (0) | 2021.08.23 |
댓글