주식 가격
나의 생각의 흐름을 주절주절..
주식의 가격이 담긴 배열(prices)의 값을 순서대로 스택에 Push해준다.
스택의 Top과 prices를 비교해서 가격이 떨어지지 않은 기간을 계산해준다.
더 이상 Push할 값이 없을 때까지 반복한다.결론
스택을 이용하여 간단하게 풀 수 있는 문제였다.
끗👨💻 소스 코드
def solution(prices): answer = [] stack = [prices[0]] i = 1 while stack: answer.append(0) for j in range(i, len(prices)): answer[i - 1] = j - i + 1 if prices[j] < stack[-1]: break stack.pop() if i < len(prices): stack.append(prices[i]) i += 1 return answer