Skip to content

Commit d143030

Browse files
committed
2 parents c81c344 + 662fe3a commit d143030

File tree

2 files changed

+272
-0
lines changed

2 files changed

+272
-0
lines changed

Diff for: 5주차/김유경/레벨2__더_맵게.js

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
function solution(scoville, K) {
2+
let answer = 0;
3+
const minHeap = new MinHeap();
4+
5+
scoville.forEach((e) => {
6+
minHeap.add(e);
7+
});
8+
9+
while (true) {
10+
const firstMin = minHeap.poll();
11+
const secondtMin = minHeap.poll();
12+
13+
if (!secondtMin) {
14+
if (firstMin < K) {
15+
answer = -1;
16+
}
17+
break;
18+
}
19+
20+
const newScoville = firstMin + secondtMin * 2;
21+
22+
if (firstMin >= K) {
23+
break;
24+
}
25+
26+
minHeap.add(newScoville);
27+
28+
answer += 1;
29+
}
30+
31+
return answer;
32+
}
33+
34+
class MinHeap {
35+
constructor() {
36+
this.items = [];
37+
}
38+
39+
swap(index1, index2) {
40+
let temp = this.items[index1]; // items의 index1의 값을 temp(임시공간)에 담음
41+
this.items[index1] = this.items[index2]; // index1에 index2의 값을 저장
42+
this.items[index2] = temp; // index2에 아까 index1의 값을 temp에 넣어놓은 값을 저장
43+
}
44+
45+
//부모 인덱스 구하는 메소드
46+
parentIndex(index) {
47+
return Math.floor((index - 1) / 2);
48+
}
49+
50+
//왼쪽 자식 인덱스 구하는 메소드
51+
leftChildIndex(index) {
52+
return index * 2 + 1;
53+
}
54+
55+
//오른쪽 자식 인덱스 구하는 메소드
56+
rightChildIndex(index) {
57+
return index * 2 + 2;
58+
}
59+
60+
//부모 노드 구하는 메소드
61+
parent(index) {
62+
return this.items[this.parentIndex(index)];
63+
}
64+
65+
//왼쪽 자식 노드 구하는 메소드
66+
leftChild(index) {
67+
return this.items[this.leftChildIndex(index)];
68+
}
69+
70+
//오른쪽 자식 노드 구하는 메소드
71+
rightChild(index) {
72+
return this.items[this.rightChildIndex(index)];
73+
}
74+
75+
//최대 힙의 경우 최댓값을 반환하고 최소 힙의 경우 최솟값을 반환하는 메소드
76+
peek() {
77+
return this.items[0];
78+
}
79+
80+
//힙의 크기(항목 개수)를 반환하는 메소드
81+
size() {
82+
return this.items.length;
83+
}
84+
85+
// MinHeap 클래스는 Heap 클래스를 상속받았으므로 Heap 클래스의 메소드를 모두 사용할 수 있다.
86+
bubbleUp() {
87+
let index = this.items.length - 1;
88+
89+
while (
90+
this.parent(index) !== undefined &&
91+
this.parent(index) > this.items[index]
92+
) {
93+
this.swap(index, this.parentIndex(index));
94+
index = this.parentIndex(index);
95+
}
96+
}
97+
98+
bubbleDown() {
99+
let index = 0;
100+
101+
while (
102+
this.leftChild(index) !== undefined &&
103+
(this.leftChild(index) < this.items[index] ||
104+
this.rightChild(index) < this.items[index])
105+
) {
106+
let smallerIndex = this.leftChildIndex(index);
107+
108+
if (
109+
this.rightChild(index) !== undefined &&
110+
this.rightChild(index) < this.items[smallerIndex]
111+
) {
112+
smallerIndex = this.rightChildIndex(index);
113+
}
114+
115+
this.swap(index, smallerIndex);
116+
117+
index = smallerIndex;
118+
}
119+
}
120+
121+
add(item) {
122+
this.items[this.items.length] = item;
123+
this.bubbleUp();
124+
}
125+
126+
poll() {
127+
let item = this.items[0]; // 첫번째 원소 keep
128+
this.items[0] = this.items[this.items.length - 1]; // 맨 마지막 원소를 첫번째 원소로 복사
129+
this.items.pop(); // 맨 마지막 원소 삭제
130+
this.bubbleDown();
131+
132+
return item; // keep해둔 값 반환
133+
}
134+
}

Diff for: 5주차/김유경/실버2__최소_힙_1927.js

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
const fs = require("fs");
2+
const filePath =
3+
process.platform === "linux" ? "/dev/stdin" : `${__dirname}/input.txt`;
4+
let input = fs.readFileSync(filePath).toString().trim().split("\n");
5+
6+
const n = Number(input.shift());
7+
const arr = input.map((x) => Number(x.trim()));
8+
const answer = [];
9+
10+
class MinHeap {
11+
constructor() {
12+
this.items = [];
13+
}
14+
15+
swap(index1, index2) {
16+
let temp = this.items[index1]; // items의 index1의 값을 temp(임시공간)에 담음
17+
this.items[index1] = this.items[index2]; // index1에 index2의 값을 저장
18+
this.items[index2] = temp; // index2에 아까 index1의 값을 temp에 넣어놓은 값을 저장
19+
}
20+
21+
//부모 인덱스 구하는 메소드
22+
parentIndex(index) {
23+
return Math.floor((index - 1) / 2);
24+
}
25+
26+
//왼쪽 자식 인덱스 구하는 메소드
27+
leftChildIndex(index) {
28+
return index * 2 + 1;
29+
}
30+
31+
//오른쪽 자식 인덱스 구하는 메소드
32+
rightChildIndex(index) {
33+
return index * 2 + 2;
34+
}
35+
36+
//부모 노드 구하는 메소드
37+
parent(index) {
38+
return this.items[this.parentIndex(index)];
39+
}
40+
41+
//왼쪽 자식 노드 구하는 메소드
42+
leftChild(index) {
43+
return this.items[this.leftChildIndex(index)];
44+
}
45+
46+
//오른쪽 자식 노드 구하는 메소드
47+
rightChild(index) {
48+
return this.items[this.rightChildIndex(index)];
49+
}
50+
51+
//최대 힙의 경우 최댓값을 반환하고 최소 힙의 경우 최솟값을 반환하는 메소드
52+
peek() {
53+
return this.items[0];
54+
}
55+
56+
//힙의 크기(항목 개수)를 반환하는 메소드
57+
size() {
58+
return this.items.length;
59+
}
60+
61+
// MinHeap 클래스는 Heap 클래스를 상속받았으므로 Heap 클래스의 메소드를 모두 사용할 수 있다.
62+
bubbleUp() {
63+
let index = this.items.length - 1;
64+
65+
while (
66+
this.parent(index) !== undefined &&
67+
this.parent(index) > this.items[index]
68+
) {
69+
this.swap(index, this.parentIndex(index));
70+
index = this.parentIndex(index);
71+
}
72+
}
73+
74+
bubbleDown() {
75+
let index = 0;
76+
77+
while (
78+
this.leftChild(index) !== undefined &&
79+
(this.leftChild(index) < this.items[index] ||
80+
this.rightChild(index) < this.items[index])
81+
) {
82+
let smallerIndex = this.leftChildIndex(index);
83+
84+
if (
85+
this.rightChild(index) !== undefined &&
86+
this.rightChild(index) < this.items[smallerIndex]
87+
) {
88+
smallerIndex = this.rightChildIndex(index);
89+
}
90+
91+
this.swap(index, smallerIndex);
92+
93+
index = smallerIndex;
94+
}
95+
}
96+
97+
add(item) {
98+
this.items[this.items.length] = item;
99+
this.bubbleUp();
100+
}
101+
102+
poll() {
103+
let item = this.items[0]; // 첫번째 원소 keep
104+
this.items[0] = this.items[this.items.length - 1]; // 맨 마지막 원소를 첫번째 원소로 복사
105+
this.items.pop(); // 맨 마지막 원소 삭제
106+
this.bubbleDown();
107+
108+
return item; // keep해둔 값 반환
109+
}
110+
}
111+
112+
const minHeap = new MinHeap();
113+
minHeap.add(1);
114+
minHeap.add(10);
115+
minHeap.add(5);
116+
minHeap.add(100);
117+
minHeap.add(8);
118+
console.log(minHeap); //array(5) [1, 8, 5, 100, 10]
119+
console.log(minHeap.poll()); // 1
120+
console.log(minHeap.poll()); // 5
121+
console.log(minHeap.poll()); // 8
122+
console.log(minHeap.poll()); // 10
123+
console.log(minHeap.poll()); // 100
124+
console.log(minHeap.poll()); // 100
125+
console.log(minHeap); // array(0)
126+
// for (let i = 0; i < n; i++) {
127+
// if (arr[i] !== 0) {
128+
// minHeap.add(arr[i]);
129+
// } else {
130+
// if (minHeap.size() === 0) {
131+
// answer.push(0);
132+
// } else {
133+
// answer.push(minHeap.poll());
134+
// }
135+
// }
136+
// }
137+
138+
// console.log(answer.join("\n"));

0 commit comments

Comments
 (0)