입력 데이터 리스트와 그 reverse한 리스트를 비교하면된다.
스택 구현부
class ArrayStack:
def __init__(self):
self.data = []
def size(self):
return len(self.data)
def isEmpty(self):
return self.size() == 0
def push(self, item):
self.data.append(item)
def pop(self):
if self.isEmpty() is not True:
return self.data.pop()
def peek(self):
if self.isEmpty() is not True:
return self.data[-1]
else:
print("Stack is empty")
return None
def print_list(self):
print(self.data)
문제 코드
리스트의 값들을 스택에 넣어주었다. 스택이 모두 비거나 i
가 리스트의 길이보다 작을동안 반복하면 된다.
n = int(input())
arr = list(map(int, input().split()))
def funGame(arr):
st = ArrayStack()
for i in arr:
st.push(i)
i=0
result =[]
while not st.isEmpty() and i < len(arr):
if arr[i] < st.peek():
i+=1
tmp=2
elif arr[i] > st.peek():
st.pop()
tmp=1
else :
i+=1
st.pop()
tmp=0
result.append(tmp)
return result
out_ = funGame(arr)
print (' '.join(map(str, out_)))
[백준] 10799번 쇠막대기 (python) (1) | 2020.05.29 |
---|