Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 프로그래머스 #NULL 처리하기
- 카카오 코테
- 카카오 #프로그래머스 #python #코딩테스트 #오픈채팅방
- 그리디알고리즘 #그리디 #백준 #우선순위큐 #최소힙 #최대힙 #알고리즘 #코딩테스트 #python
- 프로그래머스 #python #코딩테스트 #코테공부 #알고리즘 #dict
- 백준 #백준알고리즘 #알고리즘 #코딩테스트 #코딩테스트준비 #코테준비 #백준2110 #python #문제풀이
- 프로그래머스 #c++ #코딩테스트
- 프로그래머스 #네트워크 #c++ #코딩테스트 #코테 #코테준비 #dfs
- 프로그래머스 #sql #mysql #코딩테스트
- 백준 #이거다시풀기
- 프로그래머스 #python #2021카카오 #카카오코테 #카카오인턴쉽
- 동
- 백준 #백준2217 #백준로프 #python
Archives
- Today
- Total
say repository
[프로그래머스] 주식가격 python 본문
728x90
https://programmers.co.kr/learn/courses/30/lessons/42584
코딩테스트 연습 - 주식가격
초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,00
programmers.co.kr
출처 : 프로그래머스
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 |