[프로그래머스]level2-주식가격-Python[파이썬]

s2ul3·2022년 10월 6일
0

문제링크

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

문제설명

알고리즘

prices를 큐에 담고 왼쪽에서부터 원소 하나씩 뺀 값(target)과 큐에 남아있는 주식가격과 비교.
큐에 남아있는 주식가격들과 비교할 때 target값보다 작은 주식가격이 있다면 그 이후의 주식가격은 볼 필요가 없으므로 break하고 이때의 시간을 answer에 담는다.

코드

from collections import deque
def solution(prices):
    answer = []
    q = deque(prices)
    while q:
        target = q.popleft()
        fall_t = 0 # 떨어지는 시점
        for p in q:
            fall_t += 1
            if p < target:
                break
        answer.append(fall_t)                 
    return answer
profile
statistics & computer science

0개의 댓글