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

코린이·2022년 6월 4일
0

프로그래머스

목록 보기
12/22

📢 "주식가격" 문제

프로그래머스 문제 링크

🔎 풀이

사용언어 : python
dequepopleft() 함수를 이용

prices[0]를 i에 저장

for문을 이용하여 for문이 반복될 때마다 time +=1을 해주고,
만약 y가 i보다 작으면 break를 해주었다.

answer.append(time-1)에서 -1을 한 이유가 for문이 i값을 포함하여 돌기 때문이다.

🔎 코드

from collections import deque

def solution(prices):
    prices = deque(prices)
    answer= []
    while 1:
        i = prices[0]
        time = 0
        if len(prices) ==  1:
            answer.append(0)
            break
        for y in prices:
            time+=1
            if y < i:
                break

        answer.append(time-1)
        prices.popleft()
    return answer
profile
초보 개발자

0개의 댓글