Search

์กฐ์˜ˆ์ง€

2.๋ฌธ์ œ์ด๋ฆ„
3. ์ˆ˜ํ–‰์‹œ๊ฐ„[์ดˆ(s)]
1800
์ข‹์•„์š” ๋ˆ„๋ฅด๊ธฐ
์ข‹์•„์š” ์ˆ˜
: 0
5 more properties
| ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ
import heapq def solution(scoville, K): answer = 0 heapq.heapify(scoville) while scoville: now = heapq.heappop(scoville) if now < K: if scoville: second = heapq.heappop(scoville) heapq.heappush(scoville, now + 2*second) answer += 1 else: break else: return answer return -1
Python
๋ณต์‚ฌ
| ์ฝ”๋“œ ์„ค๋ช…ํ•˜๊ธฐ
heap์„ ์ด์šฉํ•˜์—ฌ ๊ฐ€์žฅ ์ž‘์€ ์›์†Œ๊ฐ€ K๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋‘๋ฒˆ์งธ ์ž‘์€ ์›์†Œ 2๋ฐฐ์™€ ๋”ํ•˜์—ฌ ๋‹ค์‹œ pushํ•˜๊ณ , K๋ณด๋‹ค ํฌ๋ฉด ์ด๋•Œ๊นŒ์ง€ ์„ž์€ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
Scoville์ด ๋๋‚˜๊ฑฐ๋‚˜ ๋‘๋ฒˆ์งธ ์„ž์„ ์ˆ˜๊ฐ€ ์—†์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.