주식 가격
코딩테스트 연습 - 주식가격
초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,00
programmers.co.kr
- 내 풀이 1
- 테스트 케이스 1개 빼고 fail
- 바로 떨어질 경우만 1초로 간주하는 줄 알았음
- → 떨어지는 경우까지 버틴 시간을 구해야 하기 때문에 if count~ 문이 없어야 함
from collections import deque
def solution(prices):
queue=deque(prices)
answer = []
while queue:
count=0
q=queue.popleft()
for a in queue:
if a<q:
if count==0:
count+=1
break
count+=1
answer.append(count)
return answer
- 내 풀이 2
- 테스트 케이스 추가 : [1, 2, 3, 2, 3, 1] → 결과: [5, 4, 1, 2, 1, 0]
- 떨어지기까지 버틴 시간을 구하기
- 덱(deque) 사용
from collections import deque
def solution(prices):
queue=deque(prices)
answer = []
while queue:
count=0
q=queue.popleft()
for a in queue:
if a<q:
count+=1
break
count+=1
answer.append(count)
return answer
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
| [프로그래머스] [Level 2] 최댓값과 최솟값 (0) | 2022.04.21 |
|---|---|
| [프로그래머스] [Level 2] 최솟값 만들기 (0) | 2022.04.20 |
| [프로그래머스] [Level 2] 피보나치 수 (0) | 2022.04.20 |
| [프로그래머스] [Level 2] 행렬의 곱셈 (0) | 2022.04.17 |
| [프로그래머스] [Level 2] [탐욕법(Greedy)] 구명보트 (0) | 2022.04.17 |