1 18258 ํ 2 ์„ฑ๊ณต 7886 26220 30.958%
ํ์˜ ๊ฐœ๋…์„ ์ตํžˆ๊ณ  ์‹ค์Šตํ•˜๋Š” ๋ฌธ์ œ. ์—ฐ์‚ฐ ๋‹น ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(1)์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์„ธ์š”.

def push(num) :
    list.append(num)

def pop() :
    if len(list) != 0 :
       print(list.popleft())
    else :
        print(-1)
def size() :
    print(len(list))

def empty() :
    if len(list) != 0 :
        print(0)
    else :
        print(1)

def front() :
    if len(list) != 0 :
        print(list[0])
    else : print(-1)

def back() :
    if len(list) != 0 :
        print(list[-1])
    else :
        print(-1)

import sys 
from collections import deque

list = deque([])
for i in range(int(sys.stdin.readline())) :
    x = sys.stdin.readline().split()
    if x[0] == 'push' :
        push(x[1])
    elif x[0] =='pop' :
        pop()
    elif x[0] == 'size' :
        size()
    elif x[0] == 'empty' :
        empty()
    elif x[0] == 'front' :
        front()
    else :
        back()

 

2 2164 ์นด๋“œ2 ์„ฑ๊ณต 15599 29699 53.860%
ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ž‘์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ

from collections import deque
import sys

n = int(sys.stdin.readline())
array = deque()
for i in range(n) :
    array.append(i+1)

while len(array) != 1 :
    array.popleft()
    temp = array.popleft()
    array.append(temp)

print(array.pop())

3 11866 ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ 0 ์„ฑ๊ณต 13135 23039 57.841%
ํ๋ฅผ ์ด์šฉํ•ด ์ œ๊ฑฐ ๊ณผ์ •์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ

import sys 
from collections import deque

array = deque()
n, k = map(int, sys.stdin.readline().split())

for i in range(n) :
    array.append(i+1)
    
print("<", end='')

while array :
    for i in range(k-1) :
        array.append(array[0])
        array.popleft()
    
    print("{}".format(array.popleft()), end='')
    if array :
        print(", ",end='')

print(">")

4 1966 ํ”„๋ฆฐํ„ฐ ํ ์„ฑ๊ณต์ถœ์ฒ˜๋‹ค๊ตญ์–ด 17461 32210 55.895%
ํ์˜ ๊ฐœ๋…์ด ์‘์šฉ๋œ ๋ฌธ์ œ

import sys
from collections import deque


t = int(sys.stdin.readline())

for _ in range(t) :
    n, m = map(int,sys.stdin.readline().split())
    #array = deque()
    array = deque(list(map(int, sys.stdin.readline().split())))
    index = deque(list(range(len(array))))

    index[m] = -1 #ํƒ€๊ฒŸ์„ -1๋กœ ์„ค์ •
    count= 0
    
    while True:
        if array[0] != max(array) :
            array.append(array[0])
            array.popleft()
            index.append(index[0])
            index.popleft()
            
        else :
            count += 1
            if index[0] == -1 :
                print(count)
                break
            else :
                array.popleft()
                index.popleft()

5 10866 ๋ฑ ์„ฑ๊ณต 16126 29856 56.452%
๋ฑ์˜ ๊ฐœ๋…์„ ์ตํžˆ๊ณ  ์‹ค์Šตํ•˜๋Š” ๋ฌธ์ œ. (์ž…๋ ฅ ํฌ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ์ž‘์•„์„œ ๋น„ํšจ์œจ์ ์ธ ๊ตฌํ˜„์œผ๋กœ๋„ ํ†ต๊ณผ๊ฐ€ ๋˜์ง€๋งŒ, ๊ฐ€๊ธ‰์ ์ด๋ฉด ์—ฐ์‚ฐ ๋‹น ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(1)์ด๋„๋ก ๊ตฌํ˜„ํ•ด ์ฃผ์„ธ์š”.)

import sys
import collections

T = int(sys.stdin.readline())
array = collections.deque([])

for _ in range(T) :
    x = sys.stdin.readline().split()
    if x[0] == "push_front" :
        array.append(x[1])
    elif x[0] == "push_back" :
        array.appendleft(x[1])
    elif x[0] == "pop_front" :
        if len(array) != 0 :
            print(array.pop())
        else :
            print(-1)
    elif x[0] == "pop_back" :
        if len(array) != 0 :
            print(array.popleft())
        else :
            print(-1)
    elif x[0] == "size" :
        print(len(array))
    elif x[0] == "empty" :
        if len(array) != 0 :
            print(0)
        else :
            print(1)
    elif x[0] == "front" :
        if len(array) != 0 :
            print(array[-1])
        else:
            print(-1)
    else :
        if len(array) != 0 :
            print(array[0])     
        else :
            print(-1)

6 1021 ํšŒ์ „ํ•˜๋Š” ํ ์„ฑ๊ณต์ถœ์ฒ˜ 10854 20657 54.086%
๋ฑ์„ ํ™œ์šฉํ•˜์—ฌ ํ๋ฅผ ํšŒ์ „์‹œํ‚ค๋Š” ๋ฌธ์ œ

import sys
import collections 

def first() :
    array.popleft()

def second() :
    global count
    temp = array.popleft()
    array.append(temp)
    count += 1

def third() :
    global count
    temp = array.pop()
    array.appendleft(temp)
    count += 1
    
count = 0

n, m = map(int, sys.stdin.readline().split())
sequnceArray = list(map(int, sys.stdin.readline().split()))

array = collections.deque(_ for _ in range(1,n+1))

for i in sequnceArray :
    while i != array[0] :
        if array.index(i) < len(array)/2 :
            second()
        else :
            third()
    first()

print(count)

7 5430 AC ์„ฑ๊ณต์ถœ์ฒ˜๋‹ค๊ตญ์–ด 10243 45928 20.242%
๋ฑ์„ ํ™œ์šฉํ•˜์—ฌ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ๋ฌธ์ œ

import sys
import collections

for _ in range(int(sys.stdin.readline())) :
    FuncArray = sys.stdin.readline() #RDD
    n = int(sys.stdin.readline()) #4
    inputArray = sys.stdin.readline()[1:-2].split(",") #1,2,3,4
    if inputArray[0] != '' :
        deque = collections.deque(inputArray) # deque[1,2,3,4]
    else :
        deque = collections.deque()

    
    leftcount= 0
    rightcount = 0
    error = False
    TF = True #TF == True > ๋ฆฌ๋ฒ„์Šคx,  TF == False > ๋ฆฌ๋ฒ„์Šคใ…‡

    for i in FuncArray : # i == R or D
        
        if i == 'R' : #๋ฆฌ๋ฒ„์Šค
            if TF == True : TF = False
            else : TF = True

        elif i == 'D' : #Drop
            if  len(deque) == 0 : #์š”์†Œ๊ฐ€ ์—†์œผ๋ฉด
                error = True  #error ์ถœ๋ ฅํ•˜๊ณ  ๋ฆฌ์…‹
                break
            else :
                if TF == True :
                    #leftcount +=1
                    deque.popleft()
                elif TF == False :
                    #rightcount +=1
                    deque.pop()

    if FuncArray.count('R') %2 != 0 : # ๋ฆฌ๋ฒ„์Šค๊ฐ€ ํ™€์ˆ˜๋ฉด 
        deque.reverse()

    if error :
        print("error")
    else :
        print("[",end="")
        for i in range(len(deque)):
            print(deque[i],end="")
            if i != len(deque)-1:
                print(",",end="")
        print("]")
728x90
๋ฐ˜์‘ํ˜•
Liky