|
| 1 | +/** |
| 2 | + * @description |
| 3 | + * brainstorming: |
| 4 | + * priority queue + result counting that priority queue was pop |
| 5 | + * |
| 6 | + * time complexity: O(n log n) |
| 7 | + * space complexity: O(n) |
| 8 | + */ |
| 9 | +var longestConsecutive = function (nums) { |
| 10 | + const queue = new CustomQueue(); |
| 11 | + let [count, before, answer] = [0, null, 0]; |
| 12 | + |
| 13 | + nums.forEach((n) => queue.insert(n)); |
| 14 | + |
| 15 | + if (queue.size === 0) return count; |
| 16 | + |
| 17 | + [count, before, answer] = [1, queue.remove(), 1]; |
| 18 | + |
| 19 | + while (queue.size) { |
| 20 | + const current = queue.remove(); |
| 21 | + |
| 22 | + if (before === current) continue; |
| 23 | + |
| 24 | + count = before + 1 === current ? count + 1 : 1; |
| 25 | + before = current; |
| 26 | + answer = Math.max(answer, count); |
| 27 | + } |
| 28 | + |
| 29 | + return answer; |
| 30 | +}; |
| 31 | + |
| 32 | +class Node { |
| 33 | + constructor(value) { |
| 34 | + this.value = value; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class CustomQueue { |
| 39 | + constructor() { |
| 40 | + this.items = new Map(); |
| 41 | + this.size = 0; |
| 42 | + } |
| 43 | + |
| 44 | + parentIndex(index) { |
| 45 | + return Math.floor((index - 1) / 2); |
| 46 | + } |
| 47 | + |
| 48 | + leftChildIndex(index) { |
| 49 | + return 2 * index + 1; |
| 50 | + } |
| 51 | + |
| 52 | + rightChildIndex(index) { |
| 53 | + return 2 * index + 2; |
| 54 | + } |
| 55 | + |
| 56 | + heapifyUp() { |
| 57 | + let index = this.size - 1; |
| 58 | + |
| 59 | + while (index > 0) { |
| 60 | + const parentIndex = this.parentIndex(index); |
| 61 | + const parent = this.items.get(parentIndex); |
| 62 | + const current = this.items.get(index); |
| 63 | + |
| 64 | + if (parent.value <= current.value) break; |
| 65 | + |
| 66 | + this.items.set(this.parentIndex(index), current); |
| 67 | + this.items.set(index, parent); |
| 68 | + |
| 69 | + index = parentIndex; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + heapifyDown() { |
| 74 | + let index = 0; |
| 75 | + |
| 76 | + while (this.leftChildIndex(index) < this.items.size) { |
| 77 | + let smallestIndex = this.leftChildIndex(index); |
| 78 | + let rightIndex = this.rightChildIndex(index); |
| 79 | + const current = this.items.get(index); |
| 80 | + |
| 81 | + if ( |
| 82 | + rightIndex < this.size && |
| 83 | + this.items.get(rightIndex).value < this.items.get(smallestIndex).value |
| 84 | + ) { |
| 85 | + smallestIndex = rightIndex; |
| 86 | + } |
| 87 | + |
| 88 | + if (current.value <= this.items.get(smallestIndex).value) break; |
| 89 | + this.items.set(index, this.items.get(smallestIndex)); |
| 90 | + this.items.set(smallestIndex, current); |
| 91 | + index = smallestIndex; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + insert(value) { |
| 96 | + const index = this.size; |
| 97 | + const node = new Node(value); |
| 98 | + this.items.set(index, node); |
| 99 | + this.size++; |
| 100 | + this.heapifyUp(); |
| 101 | + } |
| 102 | + |
| 103 | + remove() { |
| 104 | + if (this.size === 0) return null; |
| 105 | + |
| 106 | + const root = this.items.get(0); |
| 107 | + this.items.set(0, this.items.get(this.size - 1)); |
| 108 | + this.items.delete(this.size - 1); |
| 109 | + this.size--; |
| 110 | + |
| 111 | + this.heapifyDown(); |
| 112 | + return root.value; |
| 113 | + } |
| 114 | +} |
0 commit comments