728x90
https://programmers.co.kr/learn/courses/30/lessons/42584
출처 : 프로그래머스
def solution(prices):
answer = []
for i in range(len(prices)):
cnt = 0
for j in range(i+1,len(prices)):
if prices[i] <= prices[j]:
cnt += 1
else:
cnt += 1
break
answer.append(cnt)
cnt = 0
return answer
풀이 1
이중포문을 사용해서 효율성은 떨어진다.
문제 카테고리에 맞게 스택/큐를 사용해봐야겠다.
from collections import deque
def solution(prices):
answer = []
prices = deque(prices)
while prices:
p = prices.popleft()
cnt = 0
for pq in prices:
if p > pq:
cnt += 1
break
cnt += 1
answer.append(cnt)
return answer
풀이 2
큐를 이용해 풀이했다.
prices 배열을 큐로 변환해서 풀이한거 말고는 이중반복문이랑 비슷하다.
'알고리즘 문제 풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 구명보트 python (*) (0) | 2022.04.25 |
---|---|
[프로그래머스] 1차 뉴스 클러스터링 python (0) | 2022.04.24 |
[프로그래머스] 큰 수 만들기 python (*) (0) | 2022.04.21 |
[프로그래머스] N개의 최소공배수 python (0) | 2022.04.21 |
[프로그래머스] 다리를 지나는 트럭 python (0) | 2022.04.21 |