From 3efbf21b2220cfa51b14172fe363b6a5acaff701 Mon Sep 17 00:00:00 2001 From: limstonestone Date: Sat, 9 Sep 2023 22:04:11 +0900 Subject: [PATCH] =?UTF-8?q?Create=20=EB=8D=94=20=EB=A7=B5=EA=B2=8C=20dohyu?= =?UTF-8?q?n.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dohyun.py" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 "Programmers/\353\215\224 \353\247\265\352\262\214/dohyun.py" diff --git "a/Programmers/\353\215\224 \353\247\265\352\262\214/dohyun.py" "b/Programmers/\353\215\224 \353\247\265\352\262\214/dohyun.py" new file mode 100644 index 00000000..ff1801a4 --- /dev/null +++ "b/Programmers/\353\215\224 \353\247\265\352\262\214/dohyun.py" @@ -0,0 +1,31 @@ +""" + +풀이시간 +- 약 30분 + +접근법 +- 스코빌의 길이는 최대 백만 -> O(N) 에 가깝게 해결해야함 +- 최대, 최소를 넣었다 뺐다 -> 힙을 이용하면 되겠다! + +회고 +- 수민님이 저번에 보여주신 힙이 기억에 나서 풀 수 있었습니다!!! + +""" +import heapq + +def solution(scoville, K): + heapq.heapify(scoville) + answer = 0 + + while True: + not_spicy_one = heapq.heappop(scoville) + + if not_spicy_one >= K: + return answer + elif not scoville: + return -1 + else: + not_spicy_two = heapq.heappop(scoville) + new_one = not_spicy_one + not_spicy_two * 2 + heapq.heappush(scoville, new_one) + answer += 1 \ No newline at end of file