-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmorehot.js
88 lines (74 loc) · 2.22 KB
/
morehot.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class MinHeap {
constructor() {
this.heap = [];
}
getParentIndex(index) {
return Math.floor((index - 1) / 2);
}
getLeftChildIndex(index) {
return index * 2 + 1;
}
getRightChildIndex(index) {
return index * 2 + 2;
}
insert(value) {
this.heap.push(value);
this.heapifyUp();
}
heapifyUp() {
let index = this.heap.length - 1;
while (index > 0) {
let parentIndex = this.getParentIndex(index);
if (this.heap[parentIndex] > this.heap[index]) {
[this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]];
index = parentIndex;
} else {
break;
}
}
}
extractMin() {
if (this.heap.length === 1) return this.heap.pop();
const min = this.heap[0];
this.heap[0] = this.heap.pop();
this.heapifyDown();
return min;
}
heapifyDown() {
let index = 0;
while (this.getLeftChildIndex(index) < this.heap.length) {
let smallerChildIndex = this.getLeftChildIndex(index);
if (this.getRightChildIndex(index) < this.heap.length && this.heap[this.getRightChildIndex(index)] < this.heap[smallerChildIndex]) {
smallerChildIndex = this.getRightChildIndex(index);
}
if (this.heap[index] > this.heap[smallerChildIndex]) {
[this.heap[index], this.heap[smallerChildIndex]] = [this.heap[smallerChildIndex], this.heap[index]];
index = smallerChildIndex;
} else {
break;
}
}
}
size() {
return this.heap.length;
}
peek() {
return this.heap[0];
}
}
function solution(scoville, K) {
const minHeap = new MinHeap();
scoville.forEach(num => minHeap.insert(num));
let answer = 0;
while (minHeap.size() > 1 && minHeap.peek() < K) {
let first = minHeap.extractMin();
let second = minHeap.extractMin();
let newScoville = first + (second * 2);
minHeap.insert(newScoville);
answer++;
}
if (minHeap.peek() < K) {
return -1;
}
return answer;
}