algorithm/python

프로그래머스/스택,큐/주식 가격

아르르르를를르 2022. 6. 14. 22:50

https://programmers.co.kr/learn/courses/30/lessons/42584

## use deque

from collections import deque

def solution(prices):
    answer = []
    prices = deque(prices)
    
    sec = 0
    while prices:
        sec = 0
        p = prices.popleft()
        
        for price in prices:
            sec += 1
            if price < p:
                break;
        answer.append(sec)
        
    return answer
    
    
    
    

## use stack

def solution(prices):
    answer = [0] * len(prices)
    stack = []  # index가 들어갈 stack
    for i, price in enumerate(prices):
        while stack and price < prices[stack[-1]]:
            j = stack.pop()
            answer[j] = i-j
        stack.append(i)

        
    while stack:
        answer[stack[-1]] = len(prices) - stack[-1] - 1
        stack.pop()
    return answer