Solution :
## 시간복잡도 최악의 경우 O(N^2)
from collections import deque
def solution(prices):
# 초기화
q_prices=deque(prices)
answer=[]
while q_prices:
count=0
temp_value=q_prices.popleft() # 값이 나오고.
for i in q_prices:
count+=1
if temp_value >i :
break
answer.append(count)
return answer
## 시간복잡도 더 빠름.
def solution(prices):
stack = []
answer = [0] * len(prices)
for i in range(len(prices)):
while stack != [] and stack[-1][1] > prices[i]: # 가장 최근 스택 가격> 현재 가격
print(stack)
past, _ = stack.pop()
answer[past] = i - past
stack.append([i, prices[i]])
for i, s in stack:
answer[i] = len(prices) - 1 - i
return answer
Skills :
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] LV2 더 맵게 | 파이썬 (0) | 2025.05.07 |
---|---|
[프로그래머스] LV1 데이터분석 | 파이썬 (0) | 2025.05.07 |
[프로그래머스] LV2 기능개발 | 파이썬 (0) | 2025.05.07 |
[프로그래머스] LV2 프로세스 | 파이썬 (0) | 2025.05.07 |
[프로그래머스] LV2 다리를 지나는 트럭 | 파이썬 (0) | 2025.05.07 |