Coding Test/baekjoon
[백준 5430] AC - python (solved.ac - 골드 5)
조용장
2022. 4. 7. 10:56
https://www.acmicpc.net/problem/5430
5430번: AC
각 테스트 케이스에 대해서, 입력으로 주어진 정수 배열에 함수를 수행한 결과를 출력한다. 만약, 에러가 발생한 경우에는 error를 출력한다.
www.acmicpc.net
풀이
문제를 보았을때 알수있는 힌트
리스트에 담긴 데이터를 역으로 변경하고 빼고 반복한다면 컴퓨터 구조적으로 많은 시간이 걸릴 것 같다는 생각이 들었다.
최대한 시간을 줄이기위해서 리스트 보다는 덱을 통해서 작업하는것이 빠를것이라고 판단하였고 덱에서 R(뒤집기)가 나오면 pop->popleft로 바뀔수있게 코드를 작성하였다.
이렇게 해서 푸니 시간내에 답이 나온 것 같다.
from collections import deque
import sys
input = sys.stdin.readline
t= int(input())
for _ in range(t):
p = deque(list(input().strip()))
n= int(input())
x=input()[1:-2]
if len(x)!=0:
x=deque(list(map(int,x.split(","))))
reverse=False
if p.count("D")>n:
print("error")
else:
for i in p:
if i=="R":
if reverse:
reverse=False
else:
reverse=True
else:
if reverse:
x.pop()
else:
x.popleft()
print("[",end="")
while x:
if reverse:
print(x.pop(),end="")
if x:
print(",",end="")
else:
print(x.popleft(),end="")
if x:
print(",",end="")
print("]")
https://github.com/dydwkd486/coding_test/blob/main/baekjoon/baekjoon5430.py
GitHub - dydwkd486/coding_test: 코딩테스트 공부한 내용 정리
코딩테스트 공부한 내용 정리. Contribute to dydwkd486/coding_test development by creating an account on GitHub.
github.com