โ 10828 ์คํ โ
import sys
N = int(sys.stdin.readline())
stack = []*N
kinds = ['push', 'pop', 'size', 'empty', 'top']
for _ in range(N):
order = sys.stdin.readline().split()
kind = order[0]
if kind == kinds[0]:
stack.append(order[1])
elif kind == kinds[1]:
if len(stack) > 0:
print(stack[-1])
stack.pop()
else:
print(-1)
elif kind == kinds[2]:
print(len(stack))
elif kind == kinds[3]:
print(0) if len(stack) > 0 else print(1)
else:
print(stack[-1]) if len(stack) > 0 else print(-1)
๐ฅฆ stack์ list์ ํํ๋ก ์ ์ํ๊ณ ,์ฝ์ ์ append, ์ญ์ ๋ pop method๋ฅผ ์ฌ์ฉํ์๋ค. ์ฌ๊ธฐ์ ์ฃผ์ด์ง N์ ๊ธธ์ด์ ๋ง๋ stack์ ๋ฏธ๋ฆฌ ํ ๋นํด ํ ๋น ๋ฉ๋ชจ๋ฆฌ์ ์์ ์๊ฐ์ ์ต์ํ
โ 28278 ์คํ 2 โ
import sys
input=sys.stdin.readline
N=int(input())
stack=[]
for _ in range(N):
orders=list(map(int,input().strip().split()))
if orders[0] == 1:
stack.append(orders[1])
elif orders[0] == 2:
if stack:
print(stack.pop())
else:
print(-1)
elif orders[0] == 3:
print(len(stack))
elif orders[0] == 4:
if not stack: print(1)
else: print(0)
else:
if stack: print(stack[-1])
else: print(-1)
โ 9012 ๊ดํธ โ
def if_VPS(PS):
stack = []
for p in PS:
if stack:
if stack[-1] == '(' and p == ')':
stack.pop()
else:
stack.append(p)
else:
stack.append(p)
if not stack:
return 'YES'
else:
return 'NO'
for _ in range(int(input())):
PS = input()
print(if_VPS(PS))
๐งโ๏ธ PS์์ ํ ๊ฐ์ฉ stack์ ๋ฃ๋, ๊ธฐ์กด์ stack์์ '('๊ฐ ์กด์ฌํ๋ค๋ฉด, PS์์ ๋นผ๋ธ ๋ฌธ์๊ฐ ')'์ผ ๊ฒฝ์ฐ, ์๋ก ๋งค์นญ๋์ด ์์ฐ. ์ด๋ ๊ฒ ๋งค์นญ๋๋ ๊ดํธ์๋ค์ ๊ณ์ ์์ ๋ฉฐ PS ๋ชจ๋ iteration์ด ๋๋ ๋ค, stack์ ์๊ฐ ๋จ์์๋ค๋ฉด VPS๊ฐ ์๋๋ฏ๋ก NO, ์๋ค๋ฉด YES
โ 5957 Cleaning the Dishes โ
import sys
input=sys.stdin.readline
N=int(input())
unwashed=[N-i for i in range(N)]
washed_but_not_dried=[]
washed_and_dried=[]
while 1:
if len(washed_and_dried)==N: break
C_i,D_i=map(int,input().split())
if C_i==1:
for _ in range(D_i):
washed_but_not_dried.append(unwashed.pop())
else:
for _ in range(D_i):
washed_and_dried.append(washed_but_not_dried.pop())
print(*washed_and_dried[::-1],sep='\n')
๐งโ๏ธ ์ด 3๊ฐ์ stack์ ์ฌ์ฉ → unwashed, washed_but_not_dried, washed_and_dried. C_i๊ฐ 1์ด๋ฉด D_i ๊ฐ์๋งํผ unwashed์์ washed_but_not_dried stack์ผ๋ก ์ฎ๊ธฐ๊ณ , C_i๊ฐ 2์ด๋ฉด D_i ๊ฐ์๋งํผ washed_but_not_dried์์ washed_and_dried๋ก ์ฎ๊ธฐ๊ธฐ. stack ์๋ฃ๊ตฌ์กฐ์ ๋ฌผ๋ฆฌ์ ์์น์ธ ๋งจ ์๋ list์ ๋งจ ๋ค ์์, stack์ ๋ฌผ๋ฆฌ์ ๊ตฌ์กฐ์ ๋งจ ์๋ element๋ list์ ๋งจ ์ ์์๋ฅผ ๋ปํ๋ค. stack์ ๋ฌผ๋ฆฌ์ ์์น ์ ๋งจ ์๋ถํฐ ์ถ๋ ฅํ๋ ค๋ฉด list์ ๋ค ์์๋ถํฐ reversed order๋ก ์ถ๋ ฅ. stack์ ์/์๋์ list์ ๋งจ ์/๋ค ๊ตฌ๋ถ๋ง ํท๊ฐ๋ฆฌ์ง x
โ 4949 ๊ท ํ์กํ ์ธ์ โ
def is_balanced(string):
stack = []
for str in string:
if str in ['(', ')', '[', ']']:
if stack:
if (stack[-1] == '(' and str == ')') or (stack[-1] == '[' and str == ']'):
stack.pop()
else:
stack.append(str)
else:
stack.append(str)
if not stack:
return 'yes'
else:
return 'no'
while 1:
string = input()
if string == '.':
break
print(is_balanced(string))
๐งโ๏ธ ์ 9012๋ฒ ๋ฌธ์ ์ ๋์ผ
โ 10773 ์ ๋ก โ
import sys
input = sys.stdin.readline
st = []
for _ in range(int(input())):
n = int(input())
st.pop() if n == 0 else st.append(n)
print(sum(st))
๐งโ๏ธ 0์ ๋ถ๋ฅผ ๋๋ง๋ค ๊ฐ์ฅ ์ต๊ทผ์ ์๋ฅผ ์ง์ฐ๋ ๋ฐฉ์ - stack ํ์ฉ → FIFO(First In First Out)
โ 24813 The Grand Adventure โ
for _ in range(int(input())):
adventure_string=input()
backpack=[]
flag=0
for s in adventure_string:
if s in ['$','|','*']:backpack.append(s)
elif s in ['b','t','j']:
if len(backpack)==0:
print('NO')
flag+=1
break
popped=backpack.pop()
if (s=='b'and popped!='$') or (s=='t'and popped!='|') or (s=='j'and popped!='*'):
print('NO')
flag+=1
break
if flag==0:print('YES'if len(backpack)==0 else'NO')
๐งโ๏ธ ๋ฌธ์ ์์ bag๊ฐ ์ผ์ข ์ stack ์๋ฃ๊ตฌ์กฐ ์ญํ ์ ํ๋ค. ์ต๊ทผ์ ์ง์ด๋ฃ์ ๊ฒ๋ง ๊บผ๋ผ ์ ์๋ค ํ์ผ๋ฏ๋ก stack ์ฌ์ฉ. b๋ผ๋ฉด ๊บผ๋ธ ๊ฒ $๊ฐ ์๋๊ฑฐ๋, t๋ผ๋ฉด |, j๋ผ๋ฉด *๊ฐ ์๋ ๊ฒฝ์ฐ, ๋๋ ๊บผ๋ผ ๊ฒ ์๋ ๊ฒฝ์ฐ - ์ด ๋ ๊ฐ์ง if๋ฌธ์ผ๋ก yes/no ํ์
โ 3986 ์ข์ ๋จ์ด โ
import sys
input=sys.stdin.readline
def is_good_word(word):
stack=[]
for i in word:
if len(stack)!=0 and stack[-1]==i:stack.pop()
else:stack.append(i)
if len(stack)==0:return True
else: return False
n=0
for _ in range(int(input())):
if is_good_word(input().rstrip()):n+=1
print(n)
๐งโ๏ธ ์ ์ ๊ต์ฐจํ์ง ์์ผ๋ฉด์ ๋์ผํ ๊ธ์๋ผ๋ฆฌ ์ ์ด ์ฐ๊ฒฐ๋๋ค๋ ๊ฑด, stack์ ๋ง๋ค์ด์ pop()๊ณผ append() ์ฐ์ฐ์ ๋ชจ๋ ์งํํ์ ๋ ์ต์ข ์ ์ผ๋ก stack์ ๋จ๋ ์ํ๋ฒณ์ด ์๋ ๊ฒฝ์ฐ์ด๋ค. ์ง ์ง๋ ์ ํ์ stack์ ๋ง๋ค์ด์ ์ต์ข ์ ์ผ๋ก stack์ ๋ด์ฉ์ด ๋จ์์๋์ง ์๋์ง ํ์ธํ ๋ ์ฃผ๋ก ํ๋จ
โ 10845 ํ โ
from collections import deque
import sys
N = int(sys.stdin.readline())
queue = deque(maxlen=N)
kinds = ['push', 'pop', 'size', 'empty', 'front', 'back']
for _ in range(N):
order = sys.stdin.readline().split()
kind = order[0]
if kind == kinds[0]: #push X
queue.append(order[1])
elif kind == kinds[1]: #pop
print(queue.popleft()) if queue else print(-1)
elif kind == kinds[2]: #size
print(len(queue))
elif kind == kinds[3]: #empty
print(1) if len(queue) == 0 else print(0)
elif kind == kinds[4]: #front
print(queue[0]) if queue else print(-1)
else: #back
print(queue[-1]) if queue else print(-1)
๐ฅฆ deque()์ ์ธ์๋ก maxlen์ ๋ฏธ๋ฆฌ ์ค์ ํด ๋ฉ๋ชจ๋ฆฌ ์ฉ๋ ์ด๊ณผ ๋ฐฉ์ง. ์ฑ๋ฅ ์๋ ๋นจ๋ฆฌ ๊ฐ์ . append()์ popleft() ์ฌ์ฉ. popleft() ์ถ๋ ฅ๋ฌผ์ด ๋นผ๋ธ ์์ ์์ฒด๊ฐ์ ๋ฆฌํด. queue ์์ฒด๊ฐ ์กฐ๊ฑด์ด ๋์ด ์์๊ฐ queue ์์ ํ ๊ฐ ์ด์์ด๋ผ๋ ์กด์ฌํ๋ค๋ฉด True, ์์๊ฐ ์์ผ๋ฉด False
โ 18258 ํ 2 โ
import sys
input=sys.stdin.readline
from collections import deque
queue=deque()
N=int(input())
for _ in range(N):
orders=input().strip().split(' ')
if orders[0] == 'push': queue.append(orders[1])
elif orders[0] == 'pop':
if queue: print(queue.popleft())
else: print(-1)
elif orders[0] == 'size':
print(len(queue))
elif orders[0] == 'empty':
if queue: print(0)
else: print(1)
elif orders[0] == 'front':
if queue: print(queue[0])
else: print(-1)
else:
if queue: print(queue[-1])
else: print(-1)
๐ฅฆ queue์ ๋งจ ์ผ์ชฝ์ queue[0]์ผ๋ก, queue์ ๋งจ ์ค๋ฅธ์ชฝ ์์๋ queue[-1]๋ก ์ ์ ์๋ค. ๋งจ ์ผ์ชฝ์ ์ ์ผ ์ฒ์์ ๋ฃ์ ์์ / ๋งจ ์ค๋ฅธ์ชฝ์ ์ ์ผ ์ต๊ทผ, ๋ง์ง๋ง์ ๋ฃ์ ์์. ์ ๋์ ์์ ํ์ธ์ O(1)
โ 11866 ์์ธํธ์ค ๋ฌธ์ 0 โ
+++
โ 1158 ์์ธํธ์ค ๋ฌธ์ โ
from collections import deque
import sys
input = sys.stdin.readline
N,K = map(int,input().split())
q = deque([i for i in range(1,N+1)])
l = []
while len(q) != 0:
for _ in range(K-1):
q.append(q.popleft())
l.append(q.popleft())
print('<'+str(l)[1:-1]+'>')
๐ฅฆ ์ ์์ด์ ๊ฒฝ์ฐ ์ฃผ๋ก queue. N์ด 7, K๊ฐ 3์ด๋ผ๋ฉด ์ผ๋จ ๋ชจ๋ ์๋ฅผ queue์ ์ง์ด๋ฃ์ ๋ค, 1๋ฒ์งธ์ 2๋ฒ์งธ๋ ๋ค์ queue์ ๋ฃ๊ณ (์ฆ ์์ด๋ฏ๋ก ๋นผ๋ด์ ์์์ ๋ผ์คํธ ๊ผฌ๋ฆฌ์ ๋ฃ์ด์ผ ํ๊ธฐ์ queue ์๋ฃ๊ตฌ์กฐ๊ฐ ์ ๋น), 3๋ฒ์งธ๋ ๋นผ์ ์ ๋ต list์ ๋ฃ๋๋ค, queue์ ์กด์ฌํ์ง ์์ ๋๊น์ง ์ด๋ฅผ ๋ฐ๋ณต
โ 2164 ์นด๋2 โ
import sys
from collections import deque
N = int(sys.stdin.readline())
lst = [i for i in range(1,N+1)]
queue = deque(lst)
while len(queue) != 1:
queue.popleft()
queue.append(queue.popleft())
print(queue[0])
๐ฅฆ queue๋ฅผ list ์๋ฃ๊ตฌ์กฐ์์๋ ์ฌ์ฉํ ์ ์์ง๋ง, ์ฑ๋ฅ ์ธก๋ฉด์์ ๋ง์ด ๋ถ์กฑํ๋ฏ๋ก deque()์ ๋ง์ด ํ์ฉ. list์ pop(0)๊ณผ insert(0,x)๋ O(N) ํ์ง๋ง deque์ popleft(), appendleft(x) ๋ชจ๋ O(1)์ด๊ธฐ์ ๋ฃ๊ณ ๋นผ๋ ์๊ฐ์ด list์ ๋นํด ๋งค์ฐ ๋จ์ถ. ํ์ง๋ง, deque์ ๋ฌด์์ ์ ๊ทผ ์๊ฐ๋ณต์ก๋๋ O(N)์ด๋ฏ๋ก ํด๋น data๋ฅผ ๊ฐ๊ธฐ๊น์ง ๊ณ์ iteration์ด ์งํ. ๋จผ์ ๋ค์ด์จ ๊ฒ ๋จผ์ ๋๊ฐ๋, ์ฆ, ์์์ ๋นผ์ ์๋๋ก ๋ฃ๋ ๋ฌธ์ ๋ queue ํ์ฉ. 1๊ฐ ์์๋ง ๋จ์ ๋๊น์ง popleft()์ append() ๋ฐ๋ณต ์ฌ์ฉ
โ 1966 ํ๋ฆฐํฐ ํ โ
from collections import deque
import itertools
import sys
input = sys.stdin.readline
for _ in range(int(input())):
N,M=map(int,input().split())
order = 0
#queue ์์ฑ
queue=deque(maxlen=N) #N๊ฐ๊ฐ ๋ค์ด๊ฐ ์ ์๋ queue
importances=list(map(int,input().split())) #์ค์๋
for i in range(N):queue.append(importances[i]) #queue์ ์ค์๋ ์ง์ด ๋ฃ์
while queue:
others = list(itertools.islice(queue, 1, len(queue)))
if len(others) == 0: #๋ ํ๋๋ง ์๋ค๋ฉด ๋ฐ๋ก ์ถ๋ ฅ
order += 1 #์ธ์ํ๋ฏ๋ก ์ธ์ ์์ 1์ฆ๊ฐ
print(order)
break
else:
if queue[0] >= max(others): #queue์ ๊ฐ์ฅ ์์ ์๋ ๋ฌธ์ ์ธ์
M-=1 #์ํ๋ ๋ฌธ์ ์ ๋ ฌ ์์ -1
order+=1 #์ธ์ํ๋ฏ๋ก ์ธ์ ์์ 1 ์ฆ๊ฐ
queue.popleft() #์ธ์ํ๋ฏ๋ก ๋งจ ์์ ๋ฌธ์ ๋นผ๋ด๊ธฐ
if M == -1: #์ธ์ํ๋ ๋ฌธ์๊ฐ ์ํ๋ ๋ฌธ์๋ผ๋ฉด
print(order)
break
else: #์ค์๋๊ฐ ๋ ๋์ ๋ฌธ์๊ฐ ๋ค์ ์กด์ฌ
queue.append(queue.popleft()) #๋งจ ์์ ๋ฌธ์๋ฅผ ๋นผ๋ด์ ๋ค์ ๋ถ์ด๊ธฐ
M-=1 #์ํ๋ ๋ฌธ์ ์ ๋ ฌ ์์ -1
if M == -1: #์ํ๋ ๋ฌธ์๊ฐ ๋งจ ๋ค๋ก ๊ฐ๊ฒ ๋๋ค๋ฉด ํด๋น ๋ฌธ์์ ์ ๋ ฌ ์์ len(queue)-1๋ก ์ค์
M = len(queue) -1
๐ฅฆ ์ธ์๋ฅผ ์์์๋ถํฐ, ์ฆ ๋จผ์ ๋ค์ด์จ ๋ฌธ์๋ถํฐ ํ๋ ค๊ณ ํ๋, ๋๊ธฐ ์ค์ธ ๋ฌธ์๋ค์ ์ค์๋๋ณด๋ค ๋ชจ๋ ๋์์ผ ํ๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉด ๋ค๋ก ๋ณด๋ด๋ ๋ฌธ์ . ๋ด๊ฐ ์ํ๋ ๋ฌธ์์ ํ์ฌ queue ์ ๋ ฌ ์์๋ฅผ ์ ์ํ์์(M)
โ queue์ ๋ฌธ์ 1๊ฐ๋ง ์๋ค๋ฉด ๋ฐ๋ก ์ถ๋ ฅ - order+1 ํ ๋ค์ order๊ฐ ์ถ๋ ฅ -
→ queue์ ๋ฌธ์ 2๊ฐ ์ด์ ์๋ค๋ฉด
โก ๋ง์ฝ ์ค์๋๊ฐ ๋ ๋์ ๋ฌธ์๊ฐ ๋ค์ ์๋ค๋ฉด, ๋ฐ๋ก ์ถ๋ ฅ - ์ญ์ order+1ํ๊ณ , ๋ชจ๋ ๋ฌธ์๊ฐ ์ผ์ชฝ์ผ๋ก ์์ง์ด๋ฏ๋ก M ์์ - 1. ์ด ๋ ๋ด๊ฐ ์ํ๋ ๋ฌธ์๋ผ๋ฉด(M๊ฐ์ด -1์ด๋ฉด) ๋ฐ๋ก order๊ฐ ์ถ๋ ฅ
โข ์ค์๋๊ฐ ๋ ๋์ ๋ฌธ์๊ฐ ๋ค์ ์กด์ฌํ๋ค๋ฉด ์์ ๋ฌธ์ ๋นผ์ ๋ค์ ๋ถ์ด๋, queue์ append() & popleft() ์ฌ์ฉ. ์ด ๋ ๋ด๊ฐ ์ํ๋ ๋ฌธ์๊ฐ ๋ค๋ก ๊ฐ๊ฒ ๋๋ค๋ฉด M๊ฐ์ len(queue)-1๋ก ๋ฐ๋ก ๋ง์ถฐ์ค
๐ฅฆ itertools์ islice()๋ฅผ ์ด์ฉํด ํ์ฌ ๋๋ฅผ ์ ์ธ, queue์ ๋๊ธฐํ๋ ๋ค์ ๋ชจ๋ ๋ฌธ์๋ค๋ง ๋นผ์ฌ ์ ์์ (์ธ์: ๋๊ธฐ์ด,start,stop,step). ๋ด๊ฐ ์ํ๋ ๋ฌธ์๋ฅผ queue์ ์ ๋ ฌ ์์ M์ด -1์ด ๋๋ ์ง ์ฌ๋ถ์ ๋ฐ๋ผ ํ๋จํด ์ ์ธ๊ฐ์ง ์ํฉ์ ๋ง๊ฒ code ์ง์ฃผ๋ฉด ๋!
โ 10866 ๋ฑ โ
from collections import deque
import sys
dq = deque()
input = sys.stdin.readline
for _ in range(int(input())):
orders = input()
order = orders.split()[0]
if order == 'push_front':
dq.appendleft(orders.split()[1])
elif order == 'push_back':
dq.append(orders.split()[1])
elif order == 'pop_front':
if dq:
print(dq.popleft())
else:
print(-1)
elif order == 'pop_back':
if dq:
print(dq.pop())
else:
print(-1)
elif order == 'size':
print(len(dq))
elif order == 'empty':
if dq:
print(0)
else:
print(1)
elif order == 'front':
if dq:
print(dq[0])
else:
print(-1)
elif order == 'back':
if dq:
print(dq[-1])
else:
print(-1)
๐ณ ์ผ๋ ฌ๋ก ๋์ด ์๋ deque์์ ์ผ์ชฝ ์ฝ์ appendleft(), ์ค๋ฅธ์ชฝ์์ ์ฝ์ append(), ์ผ์ชฝ์์ ๋นผ๋ด๊ธฐ popleft(), ์ค๋ฅธ์ชฝ์์ ๋นผ๋ด๊ธฐ pop()
โ 28279 ๋ฑ 2 โ
import sys
input=sys.stdin.readline
from collections import deque
dq=deque()
for _ in range(int(input())):
order=input().rstrip().split()
if order[0] == '1':
dq.appendleft(order[1])
elif order[0] == '2':
dq.append(order[1])
elif order[0] == '3':
if dq:
print(dq.popleft())
else:
print(-1)
elif order[0] == '4':
if dq:
print(dq.pop())
else:
print(-1)
elif order[0] == '5':
print(len(dq))
elif order[0] == '6':
if dq: print(0)
else: print(1)
elif order[0] == '7':
if dq:
print(dq[0])
else:
print(-1)
elif order[0] == '8':
if dq:
print(dq[-1])
else:
print(-1)
๐ณ deque์ ์ ์ผ ์/๋ค ์์ ํ์ธ O(1)
โ 20301 ๋ฐ์ ์์ธํธ์ค โ
from collections import deque
N,K,M=map(int,input().split())
dq = deque(i for i in range(1,N+1))
reverse=0
while 1:
if len(dq)==0: break
if reverse==0: #์์ธํธ์ค
cnt=0
while dq:
for _ in range(K-1):
dq.append(dq.popleft())
print(dq.popleft())
cnt+=1
if cnt==M:
reverse+=1
break
if reverse==1: #๋ฐ์ ์์ธํธ์ค
cnt=0
while dq:
for _ in range(K-1):
dq.appendleft(dq.pop())
print(dq.pop())
cnt+=1
if cnt==M:
reverse-=1
break
๐ณ ์์ธํธ์ค ์์ด์ circular ์์์ queue ๋ฐฉ์์ผ๋ก ์ผ์ชฝ์์ popleft()๋ฅผ ์งํํ๊ณ , target์ด ์๋๋ฉด ์ค๋ฅธ์ชฝ์ผ๋ก append()ํ๋ ๋ฐฉ์. ๊ทธ๋ฌ๋ ๋ฐ์ ์์ธํธ์ค ์์ด์ ๊ทธ ๋ฐ๋๋ก ํ์ ํ๊ธฐ ๋๋ฌธ์ ์ค๋ฅธ์ชฝ์์ pop()์ ์งํํ๊ณ , target์ด ์๋๋ฉด ์ผ์ชฝ์ผ๋ก appendleft() / counting ๋ณ์๋ก M์ ๋๋ฌํ๋ฉด ์์ธํธ์ค ์์ด๋๋ก ์งํํ ์ง, ๋ฐ์ ์์ธํธ์ค ์์ด๋๋ก ์งํํ ์ง ํ๋จ. ์ํฉ์ ๋ฐ๋ผ ์์ชฝ์์ append, pop์ ์์ ์์ฌ๋ก ์งํํด์ผ ํ๋ฏ๋ก ์๋ฃ๊ตฌ์กฐ deque์ด ์ ๋น
โ 17952 ๊ณผ์ ๋ ๋๋์ง ์์! โ
import sys
input=sys.stdin.readline
N=int(input())
t,stack=0,[]
for _ in range(N):
info=list(map(int,input().split()))
if info[0]==1:
if info[2]==1: t+=info[1]
else:
stack.append([info[1],info[2]-1])
else:
if stack:
stack[-1][1] -= 1
if stack[-1][1] == 0:
t+=(stack.pop()[0])
print(t)
๐ณ ์ต๊ทผ ๊ณผ์ ๋ถํฐ ๋จผ์ ํด๊ฒฐํ๋ฏ๋ก ์คํ ์๋ฃ๊ตฌ์กฐ ํ์ฉ. 1์ด๋ฉด ์๋กญ๊ฒ ๋์จ ๊ณผ์ ์ฒ๋ฆฌ / 0์ด๋ฉด stack์ ์ต๊ทผ์ ๋ค์ด๊ฐ ๊ณผ์ 1๋ถ ์ฒ๋ฆฌ
โ 17891 Pairing Socks โ
import sys
input=sys.stdin.readline
n=int(input())
org=list(map(int, input().split()))
aux,cnt=[],0
for x in org:
cnt+=1
if aux:
if x != aux[-1]:
aux.append(x)
else:
aux.pop()
else:
aux.append(x)
if aux:
print('impossible')
else:
print(cnt)
๐ณ
→ ์์ฌ์๋ org for loop ๋๊ธฐ
→ ์ค๋ฅธ์ชฝ aux stack์ ์๋ฌด๊ฒ๋ ์๋ค๋ฉด aux ์๊ธฐ / ์๋ค๋ฉด aux ๋งจ ์ ์์์ org์ ๋งจ ์ ์์ ๋น๊ต: ๊ฐ๋ค๋ฉด pop(), ๋ค๋ฅด๋ค๋ฉด ์ค๋ฅธ์ชฝ aux ์๊ธฐ
โป ์ 2๋ฒ move๋ ํ์ x. pair๋ฅผ ๊ตฌํ๋ ๊ฒฝ์ฐ๋ ์ stack์ ๊ผญ๋๊ธฐ sock๊ณผ์ ๋น๊ต์ด๋ฏ๋ก ๊ตณ์ด 2๋ฒ move ์์ด 1๋ฒ move๋ง์ผ๋ก ์ถฉ๋ถ. ๋ฐ๋ผ์ org for loop ๋๋ฆฌ๋ฉด ๋๊ณ possibleํ case๋ผ๋ฉด len(org)๊ฐ ์ ๋ต. ์ ์ชฝ์ด ๋ซ๋ ค์์ง ์์, ํ ์ชฝ๋ง ๋ซ๋ฆฐ stack์ ๊ตฌ์กฐ ์ ๋น๊ต๊ฐ ์ ํ์ ์ผ๋ก ์ด๋ฃจ์ด์ ธ ์ฝ๊ฒ ํ ์ ์์๋ ๋ฌธ์
โ 4540 Q โ
import sys
input=sys.stdin.readline
from collections import deque
for _ in range(int(input())):
m,n=map(int,input().split())
l=list(input().split())
dq=deque(l)
ans=['?']*m
for _ in range(n):
x,y=map(int,input().split())
dq.remove(l[x-1])
ans[y-1]=l[x-1]
for i in range(m):
if ans[i] == '?':
ans[i] = dq.popleft()
print(*ans)
๐ณ ๋จผ์ ์ง์ ์์น์ ์ผ๋ถ ์์๋ฅผ ๋ฃ๊ณ ๋๋จธ์ง๋ queue ๋๋ฆฌ๋ฉฐ popleft()ํ ๊ฒฐ๊ณผ ์ฐจ๊ณก์ฐจ๊ณก ๋ฃ๊ธฐ
'BOJ > ๐ฅ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โ Sorting Upper-Intermediate I - 6 Solvedโ (1) | 2022.12.20 |
---|---|
โ Number Theory Intermediate I - 17 Solvedโ (0) | 2022.12.13 |
โ Recursion Intermediate - 2 Solvedโ (0) | 2022.12.11 |
โ Binary Search Intermediate I - 7 Solvedโ (0) | 2022.12.07 |
โ Greedy Intermediate I - 20 Solvedโ (0) | 2022.12.05 |
๋๊ธ