ol-rlo-zl
[프로그래머스 lv2] 주식 가격 - 스택/큐 - 파이썬(Python) 본문
문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
시행착오 (list 이용)
66.7점 - 정확성 테스트 통과, 효율성 테스트 실패(시간초과)
def solution(prices):
result = []
while prices:
now = prices.pop(0) #
i = 0
for p in prices:
i += 1
if now > p:
break
result.append(i)
return result
정답 (deque 이용)
list를 이용할 때와 같은 풀이방식이지만, deque를 이용하면 더 효율적이다.
from collections import deque
def solution(prices):
result = []
prices = deque(prices) #
while prices:
now = prices.popleft() #
i = 0
for p in prices:
i += 1
if now > p:
break
result.append(i)
return result
결과
'Programmers(Python)' 카테고리의 다른 글
[프로그래머스 lv2] 땅따먹기 - 파이썬(Python) (0) | 2023.04.05 |
---|---|
[프로그래머스 lv2] 스킬 트리 - 파이썬(Python) (0) | 2023.04.04 |
[프로그래머스 lv2] 오픈채팅방 - 파이썬(Python) (0) | 2023.04.04 |
[프로그래머스 lv2] 주차 요금 계산 - 파이썬(Python) (0) | 2023.04.03 |
[프로그래머스 lv2] [3차] n진수 게임 - 파이썬(Python) (0) | 2023.04.03 |