|
| 1 | +/** |
| 2 | + * ๋ฌธ์ ์ ํ: Heap |
| 3 | + * |
| 4 | + * ๋ฌธ์ ์ค๋ช
|
| 5 | + * - ๊ฐ์ฅ ๋น๋์๊ฐ ๋ง์ K๊ฐ Element ์ถ์ถ |
| 6 | + * |
| 7 | + * ์์ด๋์ด |
| 8 | + * 1) ๋น๋์ ๊ณ์ฐํ์ฌ index์ ํจ๊ป Map์ ์ ์ฅ, ๋น๋์๋ฅผ ๊ธฐ์ค์ผ๋ก MinHeap ๊ตฌ์ฑ |
| 9 | + * 2) ๋น๋์ Map์ ์ํํ๋ฉด์ MinHeap์ ์ถ๊ฐ + MinHeap์ ํฌ๊ธฐ๊ฐ K๋ฅผ ์ด๊ณผํ๋ฉด ์ต์๊ฐ ์ ๊ฑฐ (๋ฏธ๋ฆฌ๋ฏธ๋ฆฌ ๋น๋์๊ฐ ๋ง์ K๊ฐ Element๋ฅผ ๋ง๋ค์ด๋๊ฐ๋ ํ์) |
| 10 | + * 3) MinHeap์ ๋จ์์๋ Element๋ค์ index๋ฅผ ๋ฐํ |
| 11 | + * |
| 12 | + * ์๊ฐ ๋ณต์ก๋: O(NlogK) |
| 13 | + * ๊ณต๊ฐ ๋ณต์ก๋: O(N) |
| 14 | + */ |
| 15 | +class MinHeap { |
| 16 | + heap: [number, number][] = []; |
| 17 | + |
| 18 | + insert(item: [number, number]) { |
| 19 | + this.heap.push(item); |
| 20 | + this.bubbleUp(); |
| 21 | + } |
| 22 | + |
| 23 | + private bubbleUp() { |
| 24 | + let index = this.heap.length - 1; |
| 25 | + while (index > 0) { |
| 26 | + const parentIndex = Math.floor((index - 1) / 2); |
| 27 | + if (this.heap[index][0] >= this.heap[parentIndex][0]) break; |
| 28 | + [this.heap[index], this.heap[parentIndex]] = [ |
| 29 | + this.heap[parentIndex], |
| 30 | + this.heap[index], |
| 31 | + ]; |
| 32 | + index = parentIndex; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + extractMin(): [number, number] | undefined { |
| 37 | + if (this.heap.length === 0) return undefined; |
| 38 | + const min = this.heap[0]; |
| 39 | + const end = this.heap.pop(); |
| 40 | + if (this.heap.length > 0 && end !== undefined) { |
| 41 | + this.heap[0] = end; |
| 42 | + this.sinkDown(0); |
| 43 | + } |
| 44 | + return min; |
| 45 | + } |
| 46 | + |
| 47 | + private sinkDown(index: number) { |
| 48 | + const length = this.heap.length; |
| 49 | + while (true) { |
| 50 | + let left = 2 * index + 1; |
| 51 | + let right = 2 * index + 2; |
| 52 | + let smallest = index; |
| 53 | + |
| 54 | + if (left < length && this.heap[left][0] < this.heap[smallest][0]) { |
| 55 | + smallest = left; |
| 56 | + } |
| 57 | + if (right < length && this.heap[right][0] < this.heap[smallest][0]) { |
| 58 | + smallest = right; |
| 59 | + } |
| 60 | + if (index === smallest) break; |
| 61 | + |
| 62 | + [this.heap[index], this.heap[smallest]] = [ |
| 63 | + this.heap[smallest], |
| 64 | + this.heap[index], |
| 65 | + ]; |
| 66 | + index = smallest; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + peek(): [number, number] | undefined { |
| 71 | + return this.heap[0]; |
| 72 | + } |
| 73 | + |
| 74 | + size(): number { |
| 75 | + return this.heap.length; |
| 76 | + } |
| 77 | +} |
| 78 | +function topKFrequent(nums: number[], k: number): number[] { |
| 79 | + const freqMap = new Map<number, number>(); |
| 80 | + for (const num of nums) { |
| 81 | + freqMap.set(num, (freqMap.get(num) ?? 0) + 1); |
| 82 | + } |
| 83 | + |
| 84 | + const minHeap = new MinHeap(); |
| 85 | + for (const [num, count] of freqMap.entries()) { |
| 86 | + minHeap.insert([count, num]); |
| 87 | + if (minHeap.size() > k) { |
| 88 | + minHeap.extractMin(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return minHeap.heap.map(([_, num]) => num); |
| 93 | +} |
0 commit comments