Updated:

  • 시간복잡도를 신경쓰면서 문제에 접근
  • class 배열 구현
  • 스택을 사용하지 않고 구현

class top:
    def __init__(self, index, value):
        self.index = index
        self.value = value

N = int(input())

top_list = []

arr = list(map(int, input().split(" ")))

top_list.append(top(1,arr[0]))
print(0)

for i in range(2,N+1):
    n = arr[i-1]
    if n > top_list[0].value:
        top_list = []
        top_list.append(top(i,n))
        print(0)
        continue
    if n<top_list[len(top_list)-1].value:
        print(top_list[len(top_list)-1].index)
        top_list.append(top(i,n))
        continue
    for j in range(1, len(top_list)):
        if n > top_list[j].value:
            top_list[j:] = []
            top_list.append(top(i,n))
            print(top_list[j-1].index)
            check = False
            break
    top_list.sort(key = lambda object:object.value,reverse = True)
    

Leave a comment