초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요.
제한사항
from collections import deque
def solution(prices):
answer = []
prices = deque(prices)
while prices:
top = prices.popleft()
sec = 0
for price in prices:
sec += 1
if price < top:
break
answer.append(sec)
return answer
top = 비교할 가격
sec = 가격이 떨어지지 않은 기간
top
보다 price
가 적을때까지 sec
를 1씩 증가시킨다.
원래
for i in range(len(prices)):
if price[i] < top:
break
이런 식으로 구현했더니 시간초과로 틀렸다.
도덕책.. 시간복잡도에서 뭐가 그렇게 차이가 나는건지 여즉 모르겠다.
구현 자체는 어렵진않았는데 시간초과의 의문이 아직 남아있다.