Skip to content

Commit

Permalink
solve(programmers): LV2_주식가격_py
Browse files Browse the repository at this point in the history
# id: 문제 id를 숫자로 작성
# categories : 해당 문제의 유형을 ,로 구분하여 작성
# tags : 해당 문제의 태그를 ,로 구분하여 작성
# time : 해당 문제 풀이에 걸린 시간을 분단위 숫자로 작성
# try : 해당 문제에 몇번의 시도를 했는지 숫자로 작성
# help: 해당 문제에 외부의 도움을 받았는지 true/false로 작성
# url : 해당 문제의 url을 작성
id: 42584
categories: [자료구조]
tags: []
time: 18
try: 2
help: true
url: https://school.programmers.co.kr/learn/courses/30/lessons/42584
  • Loading branch information
gogumaC committed Aug 15, 2024
1 parent f448f89 commit ee2de76
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/programmers/LV2_주식가격.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import heapq
def solution(prices):
hq=[]
answer = [0 for _ in prices]
heapq.heapify(hq)
for t,p in enumerate(prices):
while len(hq)>0 and hq[0][1]>p:
_,_,lt=heapq.heappop(hq)
answer[lt]=(t-lt)
heapq.heappush(hq,(-p,p,t))

while hq:
_,_,lt=heapq.heappop(hq)
answer[lt]=len(prices)-lt-1

return answer

def efficiency_solution(prices):
answer = [0]*len(prices)
st=[]

for t,p in enumerate(prices):
while len(st)>0 and st[-1][0]>p:
(_,lt)=st.pop()
answer[lt]=t-lt
st.append((p,t))

while len(st)>0:
(_,lt)=st.pop()
answer[lt]=len(prices)-lt-1
return answer

0 comments on commit ee2de76

Please sign in to comment.