Skip to content

Commit 09af8cd

Browse files
committed
feat: 5주차 문제풀이
1 parent d143030 commit 09af8cd

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

Diff for: 5주차/이진아/더맵게.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
class MinHeap {
2+
constructor() {
3+
this.heap = [];
4+
}
5+
6+
size() {
7+
return this.heap.length;
8+
}
9+
10+
// 값을 힙에 추가
11+
push(value) {
12+
this.heap.push(value);
13+
let currentIndex = this.heap.length - 1;
14+
//추가한 값과 부모 노드의 값을 비교하여 오름차순 정렬 수행
15+
while (
16+
currentIndex > 0 &&
17+
this.heap[currentIndex] < this.heap[Math.floor((currentIndex - 1) / 2)]
18+
) {
19+
const temp = this.heap[currentIndex];
20+
this.heap[currentIndex] = this.heap[Math.floor((currentIndex - 1) / 2)];
21+
this.heap[Math.floor((currentIndex - 1) / 2)] = temp;
22+
currentIndex = Math.floor((currentIndex - 1) / 2);
23+
}
24+
}
25+
26+
// 최솟값을 힙에서 제거하고 반환
27+
pop() {
28+
if (this.heap.length === 0) return null;
29+
if (this.heap.length === 1) return this.heap.pop();
30+
31+
const minValue = this.heap[0];
32+
this.heap[0] = this.heap.pop();
33+
let currentIndex = 0;
34+
35+
while (currentIndex * 2 + 1 < this.heap.length) {
36+
// 현재 노드의 자식 중에서 더 작은 값을 가진 자식을 찾음
37+
let minChildIndex =
38+
currentIndex * 2 + 2 < this.heap.length &&
39+
this.heap[currentIndex * 2 + 2] < this.heap[currentIndex * 2 + 1]
40+
? currentIndex * 2 + 2
41+
: currentIndex * 2 + 1;
42+
43+
// 현재 노드의 값이 자식 노드보다 작으면 정렬이 끝난 것이므로 반복 중단
44+
if (this.heap[currentIndex] < this.heap[minChildIndex]) {
45+
break;
46+
}
47+
48+
// 현재 노드와 더 작은 자식 노드의 값을 교환
49+
const temp = this.heap[currentIndex];
50+
this.heap[currentIndex] = this.heap[minChildIndex];
51+
this.heap[minChildIndex] = temp;
52+
53+
// 다음 순회를 위해 현재 인덱스를 작은 자식의 인덱스로 업데이트
54+
currentIndex = minChildIndex;
55+
}
56+
57+
return minValue;
58+
}
59+
//힙의 최솟값을 반환
60+
peek() {
61+
return this.heap[0];
62+
}
63+
}
64+
65+
function solution(scoville, K) {
66+
// 주어진 배열을 최소힙에 넣음
67+
const minHeap = new MinHeap();
68+
for (const sco of scoville) {
69+
minHeap.push(sco);
70+
}
71+
72+
let mixedCount = 0;
73+
// 힙의 크기가 2 이상이고, 최솟값이 K 미만인 동안 반복
74+
while (minHeap.size() >= 2 && minHeap.peek() < K) {
75+
//가장 작은 두 값을 꺼내서 섞은 후 다시 힙에 넣음
76+
const first = minHeap.pop();
77+
const second = minHeap.pop();
78+
const mixedScov = first + second * 2;
79+
minHeap.push(mixedScov);
80+
mixedCount++;
81+
}
82+
//남아있는 최솟값이 K 이상이면 섞은 횟수 반환
83+
return minHeap.peek() >= K ? mixedCount : -1;
84+
}

Diff for: 5주차/이진아/최소힙.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
let fs = require("fs");
2+
let input = fs.readFileSync("/dev/stdin").toString().trim();
3+
input = input.split("\n").map(Number);
4+
const N = input.shift();
5+
6+
//변수 초기화
7+
let minheap = [],
8+
answer = "";
9+
10+
//최소힙 삽입 함수
11+
function insert(heap, num) {
12+
// 새로운 원소를 배열에 추가
13+
heap.push(num);
14+
let ind = heap.length;
15+
16+
// 부모 노드와 비교하여 오름차순 정렬 수행
17+
while (ind > 1) {
18+
if (heap[Math.floor(ind / 2) - 1] > heap[ind - 1]) {
19+
const temp = heap[ind - 1];
20+
heap[ind - 1] = heap[Math.floor(ind / 2) - 1];
21+
heap[Math.floor(ind / 2) - 1] = temp;
22+
ind = Math.floor(ind / 2);
23+
} else {
24+
break;
25+
}
26+
}
27+
// 정렬된 힙 반환
28+
return heap;
29+
}
30+
31+
//최소힙 삭제 함수
32+
function del(heap) {
33+
// 루트 노드에 마지막 원소를 대입하고 삭제
34+
heap[0] = heap[heap.length - 1];
35+
heap.pop();
36+
const len = heap.length;
37+
let ind = 1;
38+
39+
// 자식 노드와 비교하여 오름차순 정렬 수행
40+
while (ind * 2 <= len) {
41+
if (
42+
heap[ind - 1] > heap[ind * 2 - 1] &&
43+
(heap[2 * ind] === undefined || heap[ind * 2 - 1] < heap[ind * 2])
44+
) {
45+
const temp = heap[ind * 2 - 1];
46+
heap[ind * 2 - 1] = heap[ind - 1];
47+
heap[ind - 1] = temp;
48+
ind = ind * 2;
49+
} else if (heap[ind - 1] > heap[ind * 2]) {
50+
const temp = heap[ind * 2];
51+
heap[ind * 2] = heap[ind - 1];
52+
heap[ind - 1] = temp;
53+
ind = ind * 2 + 1;
54+
} else {
55+
break;
56+
}
57+
}
58+
// 정렬된 힙 반환
59+
return heap;
60+
}
61+
62+
//값 입력 및 연산
63+
input.forEach((value) => {
64+
//value가 0인 경우에는 최소 힙에서 삭제
65+
if (value === 0) {
66+
if (minheap.length > 0) {
67+
answer += `${minheap[0]}\n`;
68+
minheap = del(minheap);
69+
} else {
70+
answer += "0\n";
71+
}
72+
}
73+
//value가 0이 아닌 경우에는 최소 힙에 삽입
74+
else minheap = insert(minheap, value);
75+
});
76+
console.log(answer.trim());

0 commit comments

Comments
 (0)