유형 : 스택/큐

풀이 시간: 5분 -> .. 인터넷 검색 

def solution(prices):
    answer = [0 for i in range(len(prices))]
    popped = []
    while prices:
        curr = prices.pop(0)
        for i in range(len(popped)):
            if curr >= popped[i]:
                answer[i] += 1
        popped.append(curr)
    return answer

 

답도 틀리고 효율성 테스트에서도 시간 초과. 뭔가 다른 방법을 생각해내야 할 듯 하다. 

 

그냥 collections의 deque 라이브러리를 import하면 되는 거였다. 

 

from collections import deque

def solution(prices):
    answer = []
    queue = deque(prices)
    
    while queue:
        curr = queue.popleft()
        tmp = 0
        for q in queue:
            tmp += 1
            if curr > q:
                break
        answer.append(tmp)
    return answer

 

이렇게 하면 간단하게 풀이할 수 있다. 

복사했습니다!