유형 : 스택/큐
풀이 시간: 5분 -> .. 인터넷 검색
def solution(prices):
answer = [0 for i in range(len(prices))]
popped = []
while prices:
curr = prices.pop(0)
for i in range(len(popped)):
if curr >= popped[i]:
answer[i] += 1
popped.append(curr)
return answer
답도 틀리고 효율성 테스트에서도 시간 초과. 뭔가 다른 방법을 생각해내야 할 듯 하다.
그냥 collections의 deque 라이브러리를 import하면 되는 거였다.
from collections import deque
def solution(prices):
answer = []
queue = deque(prices)
while queue:
curr = queue.popleft()
tmp = 0
for q in queue:
tmp += 1
if curr > q:
break
answer.append(tmp)
return answer
이렇게 하면 간단하게 풀이할 수 있다.
'프로그래머스 > Lv. 2' 카테고리의 다른 글
12. H-index ★★ (정렬) (0) | 2024.07.18 |
---|---|
11. 가장 큰 수 ★★ (정렬) (0) | 2024.07.18 |
9. 다리를 지나는 트럭 ★ (스택/큐, sum함수의 시간 복잡도, deque) (0) | 2024.07.18 |
8. 프로세스 ★ (빈 배열에 대해 max: runtime error) (0) | 2024.07.17 |
7. 기능개발 ★(실수) (0) | 2024.07.17 |