본문 바로가기
코딩테스트/프로그래머스

[프로그래머스] LV2 주식가격 | 파이썬

by 애플파ol 2025. 5. 7.

Solution :

## 시간복잡도 최악의 경우 O(N^2)
from collections import deque
def solution(prices):
    # 초기화
    q_prices=deque(prices)
    answer=[]
    
    while q_prices:
        count=0
        temp_value=q_prices.popleft() # 값이 나오고.
        for i in q_prices:
            count+=1
            if temp_value >i :
                break 
        answer.append(count)

    return answer




## 시간복잡도 더 빠름.
def solution(prices):
    stack = []
    answer = [0] * len(prices)
    
    for i in range(len(prices)):
        
        while stack != [] and stack[-1][1] > prices[i]: # 가장 최근 스택 가격> 현재 가격
            print(stack)
            past, _ = stack.pop()
            answer[past] = i - past
        stack.append([i, prices[i]])
    
    for i, s in stack:
        answer[i] = len(prices) - 1 - i
    return answer

 

 

 

Skills :