Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- DesignPattern
- 디자인패턴
- spring batch 5.0
- 단기개발자코스
- 디자인 패턴
- 99클럽
- TiL
- 1주일회고
- 프로그래머스 이중우선순위큐
- 취업리부트코스
- 구글 OAuth login
- 개발자 취업
- 개발자부트캠프추천
- 코딩테스트 준비
- 프로그래머스
- Python
- 전략패턴 #StrategyPattern #디자인패턴
- jwttoken
- JavaScript
- 빈 조회 2개 이상
- infcon 2024
- jwt
- 인프콘 2024
- 항해99
- 빈 충돌
- 커스텀 헤더
- 파이썬
- KPT회고
- @FeignClient
- Spring multimodule
Archives
- Today
- Total
m1ndy5's coding blog
[자료구조] 백준 10828번 스택 with Python 본문
https://www.acmicpc.net/problem/10828
왜 이문제가 실버지..? 했는데 input()으로는 시간초과가 나는 문제였다.
input() 대신 sys.stdin.readline().rstrip() 을 사용하면 시간초과가 해결되는 문제였다.
참고로 sys.stdin.readline()을 사용하려면 import sys를 해줘야한다는 점!!
import sys
stack = []
def push(num):
stack.append(num)
def top():
if len(stack) == 0:
return -1
return stack[len(stack)-1]
def size():
return len(stack)
def empty():
if len(stack) == 0:
return 1
return 0
def pop():
if len(stack) == 0:
return -1
n = stack.pop()
return n
n = int(sys.stdin.readline().rstrip())
for i in range(n):
order = sys.stdin.readline().rstrip()
if ' ' in order:
order, num = order.split()
if order == 'push':
push(num)
elif order == 'top':
print(top())
elif order == 'size':
print(size())
elif order == 'empty':
print(empty())
elif order == 'pop':
print(pop())
'알고리즘 with python > 자료구조' 카테고리의 다른 글
[자료구조]백준 1874번 스택 수열 with Python (0) | 2023.03.22 |
---|---|
[자료구조] 백준 1406번 에디터 with Python (0) | 2023.03.22 |
[자료구조] 백준 1935번 후위 표기식2 with python (0) | 2023.03.22 |
[자료구조] 백준 10773번 제로 with python (0) | 2023.03.22 |
[자료구조] 백준 9012번 괄호 with Python (0) | 2023.03.20 |