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
- ํฌ๋กค๋ง
- JS
- ๋ฐฑ์ค
- Python
- fe
- ๊ฐ๋ฐ
- ํ๋ก๊ทธ๋๋จธ์ค
- CSS
- ํ๋ก ํธ์๋
- ํ ์ดํ๋ก์ ํธ
- ํ๋ก์ ํธ
- ๊ทธ๋ฆฌ๋
- KDT
- mongodb
- Til
- node.js
- ์๊ณ ๋ฆฌ์ฆ
- ๊ตญ๋น์ง์
- ๋๋ฆผ์ฝ๋ฉ
- ์ฝ๋ฉ์ ํ
- error
- ๋ชจ๊ฐ์ฝ
- javascript
- HTML
- ์ฝ๋ฉ
- ํ์ด์ฌ
- ๋ ธ๋ง๋์ฝ๋
- react
- ์ฝ๋ฉํ ์คํธ
- heapq
Archives
- Today
- Total
๐ฑ → ๐ณ
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ ๋งต๊ฒ - python ๋ณธ๋ฌธ
728x90
์ฝ๋
์๊ฐ์ด๊ณผ ์ฝ๋
from collections import deque
def solution(scoville, K):
answer = 0
scoville = deque(scoville)
while True:
scoville = deque(sorted(scoville))
if scoville[0] >= K:
break
v1 = scoville.popleft()
v2 = scoville.popleft()
scoville.append(v1+(v2*2))
answer += 1
return answer
์ฌ์ง์ด ๋ช๋ชํ ์คํธ ๋ฐํ์์๋ฌ๋ ๋ฐ์
deque ์ฌ์ฉํด์ ์๋ก์ด ์ค์ฝ๋น์ง์ ๋ค์ด์ฌ ๋ ๋ง๋ค ์ ๋ ฌํด์ค
๋ด๊ฐ ๋ด๋ ๊ตฌ๋ฆฐ ์ฝ๋ ..
์๊ฐ์ด๊ณผ ํด๊ฒฐ ์ฝ๋
from heapq import heapify, heappush, heappop
def solution(scoville, K):
answer = 0
heapify(scoville)
while True:
if scoville[0] >= K:
break
v1 = heappop(scoville)
v2 = heappop(scoville)
heappush(scoville, v1+(v2*2))
answer += 1
return answer
์ต์ํ์ ์ด์ฉํด์ ์๊ฐ์ด๊ณผ๋ ํด๊ฒฐํ์ง๋ง ๋ช ๊ฐ์ ํ ์คํธ์์ ๋ฐํ์์๋ฌ ๋ฐ์
ํด๊ฒฐ
from heapq import heapify, heappush, heappop
def solution(scoville, K):
answer = 0
heapify(scoville)
while True:
if scoville[0] >= K:
break
if len(scoville) <= 1 and scoville[0] < K:
return -1
v1 = heappop(scoville)
v2 = heappop(scoville)
heappush(scoville, v1+(v2*2))
answer += 1
return answer
์์ ๋ฌธ์ ์ ๋๋ก ์์ฝ์ด์ ๊ทธ๋ผ
๋ชจ๋ ์์์ ์ค์ฝ๋น ์ง์๋ฅผ K ์ด์์ผ๋ก ๋ง๋ค ์ ์๋ ๊ฒฝ์ฐ์๋ -1์ return ํฉ๋๋ค. ๋ถ๋ถ ์ ์ฉํด์ฃผ๋ ๋ฌธ์ ํด๊ฒฐ๋จ
์ฐธ๊ณ ์๋ฃ
728x90
'Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] N๊ฐ์ ์ต์๊ณต๋ฐฐ์ - Python (0) | 2023.08.13 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฒด์ก๋ณต - python (0) | 2023.08.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ธฐ๋ฅ๊ฐ๋ฐ - python (0) | 2023.08.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ฃผํ์ง ๋ชปํ ์ ์ - python (0) | 2023.08.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํ์ผ ๋๋ฒ - python (0) | 2023.08.02 |