|
| 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 | +} |
0 commit comments