-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path스택 2.py
More file actions
40 lines (31 loc) · 707 Bytes
/
스택 2.py
File metadata and controls
40 lines (31 loc) · 707 Bytes
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
30
31
32
33
34
35
36
37
38
39
40
import sys
input = sys.stdin.readline
stack = []
com = [0]
N = int(input())
cnt = 0
tmp = 0
for i in range(N):
com = list(map(int, input().split()))
if com[0] == 1: # push
stack.append(com[1])
cnt += 1
elif com[0] == 2: # pop
if cnt > 0:
tmp = stack.pop()
print(tmp)
cnt -= 1
else:
print(-1)
elif com[0] == 3: # count
print(cnt)
elif com[0] == 4: # empty
if cnt > 0:
print(0)
else:
print(1)
else: # top
if cnt > 0:
print(stack[-1])
else:
print(-1)