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 | 31 |
Tags
- Python
- GDSC
- programers
- baekjoon
- 현대sw
- 다트
- 안드로이드
- MVVM
- 소프티어
- 코테
- 자바
- DART
- softeer
- 머신러닝
- Flutter
- aws
- 코틀린
- 개발
- kotlin
- Android
- 플러터
- 백준
- 스위프트
- java
- 파이썬
- 알고리즘
- SWIFT
- VSCode
- 프로그래머스
- 아마존 웹 서비스
Archives
- Today
- Total
조준장 개발자 생존기
[baekjoon] 백준 18258번(파이썬): 큐 2 본문
문제
18258번: 큐 2
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
코드
import sys
from collections import deque
n = int(sys.stdin.readline())
queue = deque()
for i in range(n):
order = sys.stdin.readline().split()
if order[0] == "pop":
if queue:
print(queue.popleft())
else:
print(-1)
elif order[0] == "size":
print(len(queue))
elif order[0] == "empty":
if queue:
print(0)
else:
print(1)
elif order[0] == "front":
if queue:
print(queue[0])
else:
print(-1)
elif order[0] == "back":
if queue:
print(queue[-1])
else:
print(-1)
elif order[0] == "push":
queue.append(order[1])
실패한 코드(시간 초과)
import sys
from collections import deque
n = int(sys.stdin.readline())
queue = deque()
for i in range(n):
order = list(map(str, sys.stdin.readline().split()))
if order[0] == "pop":
if queue:
print(queue.popleft())
else:
print(-1)
elif order[0] == "size":
print(len(queue))
elif order[0] == "empty":
if queue:
print(0)
else:
print(1)
elif order[0] == "front":
if queue:
print(queue[0])
else:
print(-1)
elif order[0] == "back":
if queue:
print(queue[-1])
else:
print(-1)
elif order[0] == "push":
queue.append(order[1])
명령을 입력 받을 때 리스트 형식으로 받아서 시간 초과가 난 것을 보인다.
리스트로 입력 받는 것보다 문자열로 입력 받는게 시간 복잡도가 더 효율적인가 보다.
github
GitHub - junjange/CodingTest: 내가 푼 코딩 테스트 문제와 해결법
내가 푼 코딩 테스트 문제와 해결법. Contribute to junjange/CodingTest development by creating an account on GitHub.
github.com
'CodingTest > Baekjoon' 카테고리의 다른 글
[baekjoon] 백준 11286번(파이썬): 절댓값 힙 (0) | 2021.09.29 |
---|---|
[baekjoon] 백준 5430번(파이썬): AC (0) | 2021.09.28 |
[baekjoon] 백준 1717번(파이썬): 집합의 표현 (0) | 2021.09.26 |
[baekjoon] 백준 1021번(파이썬): 회전하는 큐 (0) | 2021.09.25 |
[baekjoon] 백준 1764번(파이썬): 듣보잡 (0) | 2021.09.24 |