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
๋ฐ์ํ