Algorithm/Python

[프로그래머스] 주식가격

🥭맹2 2021. 5. 20. 10:23

1. 문제

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

2. 접근 방법

스택을 사용해서 접근했습니다

 

3. 코드

Python

def solution(prices):
    N = len(prices)
    answer = [0]*N
    stack = []

    for idx, price in enumerate(prices):
        while stack and prices[stack[-1]] > price:
            j = stack.pop()
            answer[j] = idx-j
        stack.append(idx)
    
    for i in stack:
        answer[i] = N-i-1
    
    return answer

4. 마치며

 

'Algorithm > Python' 카테고리의 다른 글

[백준 11723] 집합  (1) 2021.05.26
[백준 10819] 차이를 최대로  (0) 2021.05.25
[프로그래머스] 튜플  (0) 2021.05.14
[백준 6603] 로또  (0) 2021.05.11
[백준 10974] 모든 순열  (0) 2021.05.10