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

 

'algorithm > python' 카테고리의 다른 글

프로그래머스/스택,큐/기능 개발  (0) 2022.06.20
프로그래머스/스택,큐/프린터  (0) 2022.06.20
[python] 최대 int 범위  (0) 2022.03.27
codility/lesson5/prefix sums/passingCars  (0) 2022.02.14
백준/2108/통계학  (0) 2021.10.27