https://school.programmers.co.kr/learn/courses/30/lessons/42626?language=python3
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
โ 1์ฐจ ํ์ด
import heapq
min_heap = []
def solution(scoville, K):
heapq.heapify(scoville)
count = 0
while scoville:
# popํ ๊ฐ์ด k์ด์์ด๋๋ฉด, while ์ข
๋ฃ
min_value = heapq.heappop(scoville)
if min_value >= K:
return count
if len(scoville) == 1:
return count
# ์์ด์ ์๋ก์ด ์ค์ฝ๋น ๊ตฌํ๊ธฐ
sec_min = heapq.heappop(scoville)
new_scoville = min_value + (sec_min * 2)
count += 1
heapq.heappush(scoville, new_scoville)
return count
๋ฌธ์ ํ๋ฆ์ ๋ฐ๋ผ, ์ฝ๋๋ก ์ฎ๊ฒจ์ ์์ฑํด๋ณด์๋ค.
์ต์ํ์ ์ฌ์ฉํ๋ฉด ๊ตฌํ์ ์ด๋ ค์์ด ์๋ ๋ฌธ์ ์๋ค. ํ์ง๋ง ๊ณ์ ํต๊ณผํ์ง ๋ชปํ๋ tc๊ฐ ์์๋ค. ์ฒ์์ heappop์ ์ํด ์์๊ฐ 1๊ฐ๊ฐ ๋จ์์์๋๋ฅผ ์๊ฐํ์ง ๋ชปํด์, ๊ทธ๋ฐ๊ฐ๋ณด๋คํ๊ณ , ์์์ ๊ฐ์๊ฐ ํ๊ฐ๋ง ๋จ์์๋ ์ด์ฐจํผ ์๋ก์ด ์ค์ฝ๋น ์์์ ๋ง๋ค์ ์๊ธฐ ๋๋ฌธ์ ์ฌํ ์์ ์นด์ดํธ๋ฅผ ๋ฆฌํดํ๋ early return ์กฐ๊ฑด์ ์ถ๊ฐ์์ผ์คฌ๋ค.
ํ์ง๋ง , ๊ทธ๋๋ ์ค๋ต์ด ๋ฐ์ํด์ ๋ค์ ๋ฌธ์ ๋ฅผ ์ฝ์ด๋ณด๋.......
๊ฒฐ๊ตญ K์ด์์ ์ค์ฝ๋น ์์์ ๋ง๋ค์์๋ค๋ฉด, -1์ ๋ฆฌํดํด์ผํ๋ค.
๋ฌธ์ ๋ฅผ ์ข ๋๋ฐ๋ก ์ฝ์ด๋! ์ ๋ ๋จน๊ณ ์ฝํ ํ๋ ค๊ณ ํ๋ ์ ์ผ ์ง์ค๋ ฅ์ด ๋จ์ด์ง๋๊ฒ ๊ฐ๋คใ ใ
โ ์ ๋ต ํ์ด
import heapq
def solution(scoville, K):
heapq.heapify(scoville)
count = 0
while len(scoville) >1:
# ์ต์๊ฐ์ด k์ด์์ด๋๋ฉด, while ์ข
๋ฃ
min_value = heapq.heappop(scoville)
if min_value >= K:
return count
# 1,2๋ฒ์งธ min๊ฐ ์์ด์ ์๋ก์ด ์ค์ฝ๋น ๊ตฌํ๊ธฐ
sec_min = heapq.heappop(scoville)
new_scoville = min_value + (sec_min * 2)
count += 1
heapq.heappush(scoville, new_scoville)
# ๊ฐ์ฅ ์๋งค์ด ์ค์ฝ๋น์ด K์ด์์ธ์ง ํ์ธ (์ถฉ์กฑ๋์ง ์๋๋ค๋ฉด, -1 ๋ฆฌํด)
if scoville[0]>=K:
return count
return -1
while๋ฌธ์ผ๋ก ์นด์ดํธ๋ฅผ ์ข ๋ฃ ํ ํ, ์ต์๊ฐ (scovile[0])์ด K ์ด์์ธ์ง ์ฌ๋ถ์ ๋ฐ๋ผ ์ต์ข ๊ฐ์ ๋ฆฌํดํด์ฃผ๋ฉด๋๋ค.
๐ BEST ํ์ด
import heapq
def solution(scoville, K):
heapq.heapify(scoville)
count = 0
while True:
first = heapq.heappop(scoville)
if first >= K:
break
if len(scoville)<1:
return -1
second = heapq.heappop(scoville)
count += 1
heapq.heappush(scoville, first + (second * 2))
return count
๋ก์ง์ ๋์ผํ์ง๋ง, -1์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง์ ๋ฐ๋ฅธ ์กฐ๊ฑด๋ฌธ์ ์ฝ๊ฐ์ ์ฐจ์ด๊ฐ ์๋ค.
โ๏ธK์ด์์ ์ค์ฝ๋น์ ๋ง์กฑํ ๋ ๊น์ง, ํ ์์ ์์๋ฅผ ๊ณ์ popํด๊ฐ๋ฉด์ ๋น๊ตํ๊ธฐ ๋๋ฌธ์
๋ง์กฑํ์ง ์๋ -1์ ๋ฆฌํดํด์ผํ๋ ๊ฒฝ์ฐ scovile ํ์ ๊ธธ์ด๊ฐ 0์ด ๋ ๊ฒ์ด๋ค.
โ JS ํ์ด
class MinHeap {
constructor() {
this.heap = [];
}
size() {
return this.heap.length;
}
// ๊ฐ์ ๋ฃ๋, ์ค๋ฆ์ฐจ ์ ์ ๋ ฌํจ
push(value) {
this.heap.push(value);
let currentIndex = this.heap.length - 1;
while (
currentIndex > 0 &&
this.heap[currentIndex] < this.heap[Math.floor((currentIndex - 1) / 2)]
) {
const temp = this.heap[currentIndex];
this.heap[currentIndex] = this.heap[Math.floor((currentIndex - 1) / 2)];
this.heap[Math.floor((currentIndex - 1) / 2)] = temp;
currentIndex = Math.floor((currentIndex - 1) / 2);
}
}
// ๊ฐ์ ๋นผ๋, ์ค๋ฆ์ฐจ ์ ์ ๋ ฌ ํจ
pop() {
if (this.heap.length === 0) return null;
if (this.heap.length === 1) return this.heap.pop();
const minValue = this.heap[0];
this.heap[0] = this.heap.pop();
let currentIndex = 0;
while (currentIndex * 2 + 1 < this.heap.length) {
let minChildIndex = currentIndex * 2 + 2 < this.heap.length && this.heap[currentIndex * 2 + 2] < this.heap[currentIndex * 2 + 1] ? currentIndex * 2 + 2 : currentIndex * 2 + 1;
if (this.heap[currentIndex] < this.heap[minChildIndex]) {
break;
}
const temp = this.heap[currentIndex];
this.heap[currentIndex] = this.heap[minChildIndex];
this.heap[minChildIndex] = temp;
currentIndex = minChildIndex;
}
return minValue;
}
peek() {
return this.heap[0];
}
}
function solution(scoville, K) {
var answer = 0;
const minHeap=new MinHeap();
//minHeap ์์ฑ
for(const sco of scoville){
minHeap.push(sco);
}
while(minHeap.size()>=2 && minHeap.peek()<K){
//1,2๋ฒ์งธ ์์ ์ค์ฝํ ๊ตฌํด์ ์๋ก์ด ์ค์ฝ๋น ์์ ๋ง๋ค๊ธฐ
const first=minHeap.pop();
const second=minHeap.pop();
const mixedFood=first+(second*2);
//์๋ก์ด ์ค์ฝํ ์์ heap์ ๋ฃ๊ธฐ
minHeap.push(mixedFood);
answer+=1;
}
return minHeap.peek()>=K ? answer : -1
}
์๋ฐ์คํฌ๋ฆฝํธ๋ heap์ ํด๋์ค๋ก ๊ตฌํํด์ผํ๊ธฐ ๋๋ฌธ์, ์ฒด๊ฐ์ ํจ์ฌ ์ด๋ ต๊ณ ๋ณต์กํด์ง๋ค ๐
'ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค Lv.1] ์นด๋๋ญ์น (0) | 2024.08.01 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค Lv.3] ์ด์ค ์ฐ์ ์์ํ (0) | 2024.07.31 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] ๊ธฐ๋ฅ๊ฐ๋ฐ (0) | 2024.07.29 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ ํ๋ฒํธ ๋ชฉ๋ก (0) | 2024.07.26 |
[ํ๋ก๊ทธ๋๋จธ์ค] JadenCase ๋ฌธ์์ด ๋ง๋ค๊ธฐ (0) | 2024.07.25 |
https://school.programmers.co.kr/learn/courses/30/lessons/42626?language=python3
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
โ 1์ฐจ ํ์ด
import heapq
min_heap = []
def solution(scoville, K):
heapq.heapify(scoville)
count = 0
while scoville:
# popํ ๊ฐ์ด k์ด์์ด๋๋ฉด, while ์ข
๋ฃ
min_value = heapq.heappop(scoville)
if min_value >= K:
return count
if len(scoville) == 1:
return count
# ์์ด์ ์๋ก์ด ์ค์ฝ๋น ๊ตฌํ๊ธฐ
sec_min = heapq.heappop(scoville)
new_scoville = min_value + (sec_min * 2)
count += 1
heapq.heappush(scoville, new_scoville)
return count
๋ฌธ์ ํ๋ฆ์ ๋ฐ๋ผ, ์ฝ๋๋ก ์ฎ๊ฒจ์ ์์ฑํด๋ณด์๋ค.
์ต์ํ์ ์ฌ์ฉํ๋ฉด ๊ตฌํ์ ์ด๋ ค์์ด ์๋ ๋ฌธ์ ์๋ค. ํ์ง๋ง ๊ณ์ ํต๊ณผํ์ง ๋ชปํ๋ tc๊ฐ ์์๋ค. ์ฒ์์ heappop์ ์ํด ์์๊ฐ 1๊ฐ๊ฐ ๋จ์์์๋๋ฅผ ์๊ฐํ์ง ๋ชปํด์, ๊ทธ๋ฐ๊ฐ๋ณด๋คํ๊ณ , ์์์ ๊ฐ์๊ฐ ํ๊ฐ๋ง ๋จ์์๋ ์ด์ฐจํผ ์๋ก์ด ์ค์ฝ๋น ์์์ ๋ง๋ค์ ์๊ธฐ ๋๋ฌธ์ ์ฌํ ์์ ์นด์ดํธ๋ฅผ ๋ฆฌํดํ๋ early return ์กฐ๊ฑด์ ์ถ๊ฐ์์ผ์คฌ๋ค.
ํ์ง๋ง , ๊ทธ๋๋ ์ค๋ต์ด ๋ฐ์ํด์ ๋ค์ ๋ฌธ์ ๋ฅผ ์ฝ์ด๋ณด๋.......
๊ฒฐ๊ตญ K์ด์์ ์ค์ฝ๋น ์์์ ๋ง๋ค์์๋ค๋ฉด, -1์ ๋ฆฌํดํด์ผํ๋ค.
๋ฌธ์ ๋ฅผ ์ข ๋๋ฐ๋ก ์ฝ์ด๋! ์ ๋ ๋จน๊ณ ์ฝํ ํ๋ ค๊ณ ํ๋ ์ ์ผ ์ง์ค๋ ฅ์ด ๋จ์ด์ง๋๊ฒ ๊ฐ๋คใ ใ
โ ์ ๋ต ํ์ด
import heapq
def solution(scoville, K):
heapq.heapify(scoville)
count = 0
while len(scoville) >1:
# ์ต์๊ฐ์ด k์ด์์ด๋๋ฉด, while ์ข
๋ฃ
min_value = heapq.heappop(scoville)
if min_value >= K:
return count
# 1,2๋ฒ์งธ min๊ฐ ์์ด์ ์๋ก์ด ์ค์ฝ๋น ๊ตฌํ๊ธฐ
sec_min = heapq.heappop(scoville)
new_scoville = min_value + (sec_min * 2)
count += 1
heapq.heappush(scoville, new_scoville)
# ๊ฐ์ฅ ์๋งค์ด ์ค์ฝ๋น์ด K์ด์์ธ์ง ํ์ธ (์ถฉ์กฑ๋์ง ์๋๋ค๋ฉด, -1 ๋ฆฌํด)
if scoville[0]>=K:
return count
return -1
while๋ฌธ์ผ๋ก ์นด์ดํธ๋ฅผ ์ข ๋ฃ ํ ํ, ์ต์๊ฐ (scovile[0])์ด K ์ด์์ธ์ง ์ฌ๋ถ์ ๋ฐ๋ผ ์ต์ข ๊ฐ์ ๋ฆฌํดํด์ฃผ๋ฉด๋๋ค.
๐ BEST ํ์ด
import heapq
def solution(scoville, K):
heapq.heapify(scoville)
count = 0
while True:
first = heapq.heappop(scoville)
if first >= K:
break
if len(scoville)<1:
return -1
second = heapq.heappop(scoville)
count += 1
heapq.heappush(scoville, first + (second * 2))
return count
๋ก์ง์ ๋์ผํ์ง๋ง, -1์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง์ ๋ฐ๋ฅธ ์กฐ๊ฑด๋ฌธ์ ์ฝ๊ฐ์ ์ฐจ์ด๊ฐ ์๋ค.
โ๏ธK์ด์์ ์ค์ฝ๋น์ ๋ง์กฑํ ๋ ๊น์ง, ํ ์์ ์์๋ฅผ ๊ณ์ popํด๊ฐ๋ฉด์ ๋น๊ตํ๊ธฐ ๋๋ฌธ์
๋ง์กฑํ์ง ์๋ -1์ ๋ฆฌํดํด์ผํ๋ ๊ฒฝ์ฐ scovile ํ์ ๊ธธ์ด๊ฐ 0์ด ๋ ๊ฒ์ด๋ค.
โ JS ํ์ด
class MinHeap {
constructor() {
this.heap = [];
}
size() {
return this.heap.length;
}
// ๊ฐ์ ๋ฃ๋, ์ค๋ฆ์ฐจ ์ ์ ๋ ฌํจ
push(value) {
this.heap.push(value);
let currentIndex = this.heap.length - 1;
while (
currentIndex > 0 &&
this.heap[currentIndex] < this.heap[Math.floor((currentIndex - 1) / 2)]
) {
const temp = this.heap[currentIndex];
this.heap[currentIndex] = this.heap[Math.floor((currentIndex - 1) / 2)];
this.heap[Math.floor((currentIndex - 1) / 2)] = temp;
currentIndex = Math.floor((currentIndex - 1) / 2);
}
}
// ๊ฐ์ ๋นผ๋, ์ค๋ฆ์ฐจ ์ ์ ๋ ฌ ํจ
pop() {
if (this.heap.length === 0) return null;
if (this.heap.length === 1) return this.heap.pop();
const minValue = this.heap[0];
this.heap[0] = this.heap.pop();
let currentIndex = 0;
while (currentIndex * 2 + 1 < this.heap.length) {
let minChildIndex = currentIndex * 2 + 2 < this.heap.length && this.heap[currentIndex * 2 + 2] < this.heap[currentIndex * 2 + 1] ? currentIndex * 2 + 2 : currentIndex * 2 + 1;
if (this.heap[currentIndex] < this.heap[minChildIndex]) {
break;
}
const temp = this.heap[currentIndex];
this.heap[currentIndex] = this.heap[minChildIndex];
this.heap[minChildIndex] = temp;
currentIndex = minChildIndex;
}
return minValue;
}
peek() {
return this.heap[0];
}
}
function solution(scoville, K) {
var answer = 0;
const minHeap=new MinHeap();
//minHeap ์์ฑ
for(const sco of scoville){
minHeap.push(sco);
}
while(minHeap.size()>=2 && minHeap.peek()<K){
//1,2๋ฒ์งธ ์์ ์ค์ฝํ ๊ตฌํด์ ์๋ก์ด ์ค์ฝ๋น ์์ ๋ง๋ค๊ธฐ
const first=minHeap.pop();
const second=minHeap.pop();
const mixedFood=first+(second*2);
//์๋ก์ด ์ค์ฝํ ์์ heap์ ๋ฃ๊ธฐ
minHeap.push(mixedFood);
answer+=1;
}
return minHeap.peek()>=K ? answer : -1
}
์๋ฐ์คํฌ๋ฆฝํธ๋ heap์ ํด๋์ค๋ก ๊ตฌํํด์ผํ๊ธฐ ๋๋ฌธ์, ์ฒด๊ฐ์ ํจ์ฌ ์ด๋ ต๊ณ ๋ณต์กํด์ง๋ค ๐
'ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค Lv.1] ์นด๋๋ญ์น (0) | 2024.08.01 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค Lv.3] ์ด์ค ์ฐ์ ์์ํ (0) | 2024.07.31 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] ๊ธฐ๋ฅ๊ฐ๋ฐ (0) | 2024.07.29 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ ํ๋ฒํธ ๋ชฉ๋ก (0) | 2024.07.26 |
[ํ๋ก๊ทธ๋๋จธ์ค] JadenCase ๋ฌธ์์ด ๋ง๋ค๊ธฐ (0) | 2024.07.25 |