문제의 입력이나 출력이 많아질 경우
파이썬은 시간초과가 발생할 수 있다.
sys를 import 해주고
input 대신 sys.stdin.readline()
print 대신 sys.stout.write()를 사용하자
sys.stdin.readline()은 개행문자도 같이 받아오니 주의(입력후 엔터 "\n"도 입력으로 받아온다)
sys.stdin.readline().strip()으로 개행문자를 제거 해줄수 있다
사용 예시
import sys
N = int(sys.stdin.readline())
for i in range(N):
command = sys.stdin.readline().strip()
if command[:4] == 'push': # push
X = int(command[5:])
command = command[:4]
que_state.push(X)
elif command[:] == 'front': # front
sys.stdout.write(f"{que_state.front()}\n")
[백준 2805번] | 나무 자르기 | 이분탐색, 이진탐색, Binary search (파이썬) (0) | 2025.02.15 |
---|---|
[백준 11659번] | 구간합, 누적합(prefix sum) | DP (파이썬) (0) | 2025.02.10 |
[백준 1003번] | 피보나치 함수 | DP (파이썬) (0) | 2025.02.09 |
파이썬 다이나믹 프로그래밍(Python Dynamic Programing) | DP (0) | 2025.02.06 |
Python EOF 해결 (0) | 2025.01.20 |