프로그래머스 스택/큐 Level 2. 주식가격 파이썬 풀이

minan·2021년 6월 21일
0

프로그래머스

목록 보기
12/92

문제링크 https://programmers.co.kr/learn/courses/30/lessons/42584

프로그래머스 스택/큐 Level 2. 주식가격 파이썬 풀이

def solution(prices):
    answer = []
    
    # 첫 번째 문자부터 마지막 앞 문자까지
    for i in range(len(prices)-1):
        # 앞에서 선택한 문자부터 마지막문자까지
        for j in range(i+1, len(prices)):
            if prices[i] > prices[j]: # 가격이 내려갔다면
                answer.append(j-i) # 결과 리스트에 시간 추가
                break
            # 가격이 오르거나 같고 마지막 문자라면
            elif prices[i] <= prices[j] and j == len(prices)-1: 
                answer.append(len(prices)-i-1) # 결과 리스트에 마지막까지의 시간 추가
                break
            # 가격이 오른다면
            else:
                continue
            
    # 마지막 문자 처리         
    answer.append(0)
        
    return answer
profile
https://github.com/minhaaan

0개의 댓글