diff --git a/README.md b/README.md index c8145ca..7099ea4 100644 --- a/README.md +++ b/README.md @@ -221,12 +221,14 @@ Ace Coding Interview with 75 Qs [1926]: ./src/page-18/1926.%20Nearest%20Exit%20from%20Entrance%20in%20Maze/nearestExit.ts [994]: ./src/page-10/994.%20Rotting%20Oranges/orangesRotting.ts -| Heap / Priority Queue | | | -| ------------------------------------- | -------- | ------ | -| 215. Kth Largest Element in an Array | Solution | Medium | -| 2336. Smallest Number in Infinite Set | Solution | Medium | -| 2542. Maximum Subsequence Score | Solution | Medium | -| 2462. Total Cost to Hire K Workers | Solution | Medium | +| Heap / Priority Queue | | | +| ------------------------------------- | --------------- | ------ | +| 215. Kth Largest Element in an Array | [Solution][215] | Medium | +| 2336. Smallest Number in Infinite Set | Solution | Medium | +| 2542. Maximum Subsequence Score | Solution | Medium | +| 2462. Total Cost to Hire K Workers | Solution | Medium | + +[215]: ./src/page-2/215.%20Kth%20Largest%20Element%20in%20an%20Array/findKthLargest.ts | Binary Search | | | | -------------------------------------------- | -------- | ------ | diff --git a/src/page-2/215. Kth Largest Element in an Array/findKthLargest.test.ts b/src/page-2/215. Kth Largest Element in an Array/findKthLargest.test.ts new file mode 100644 index 0000000..b706fbc --- /dev/null +++ b/src/page-2/215. Kth Largest Element in an Array/findKthLargest.test.ts @@ -0,0 +1,13 @@ +import { findKthLargest, findKthLargest2 } from './findKthLargest'; + +describe('215. Kth Largest Element in an Array', () => { + test('findKthLargest', () => { + expect(findKthLargest([3, 2, 1, 5, 6, 4], 2)).toBe(5); + expect(findKthLargest([3, 2, 3, 1, 2, 4, 5, 5, 6], 4)).toBe(4); + }); + + test('findKthLargest2', () => { + expect(findKthLargest2([3, 2, 1, 5, 6, 4], 2)).toBe(5); + expect(findKthLargest2([3, 2, 3, 1, 2, 4, 5, 5, 6], 4)).toBe(4); + }); +}); diff --git a/src/page-2/215. Kth Largest Element in an Array/findKthLargest.ts b/src/page-2/215. Kth Largest Element in an Array/findKthLargest.ts new file mode 100644 index 0000000..f6c783e --- /dev/null +++ b/src/page-2/215. Kth Largest Element in an Array/findKthLargest.ts @@ -0,0 +1,118 @@ +type findKthLargest = (nums: number[], k: number) => number; + +/** + * Accepted + */ +export const findKthLargest: findKthLargest = (nums, k) => { + class MinHeap { + private heap: number[]; + + constructor() { + this.heap = []; + } + + // Helper methods to calculate the indices of parent and children + private getParentIndex(index: number): number { + return Math.floor((index - 1) / 2); + } + + private getLeftChildIndex(index: number): number { + return 2 * index + 1; + } + + private getRightChildIndex(index: number): number { + return 2 * index + 2; + } + + // Method to add an element to the heap + public add(val: number): void { + this.heap.push(val); + this.heapifyUp(); + } + + // Method to remove the minimum element (root) from the heap + public poll(): number | undefined { + if (this.heap.length === 0) return undefined; + if (this.heap.length === 1) return this.heap.pop(); + + const root = this.heap[0]; + const lastElement = this.heap.pop(); // Remove the last element safely + + if (lastElement !== undefined) { + this.heap[0] = lastElement; // Only reassign if lastElement exists + this.heapifyDown(); + } + + return root; + } + + // Method to return the minimum element without removing it + public peek(): number | undefined { + return this.heap.length > 0 ? this.heap[0] : undefined; + } + + // Method to return the size of the heap + public size(): number { + return this.heap.length; + } + + // Heapify up to maintain the heap property after adding an element + private heapifyUp(): void { + let index = this.heap.length - 1; + + while (index > 0 && this.heap[this.getParentIndex(index)] > this.heap[index]) { + [this.heap[this.getParentIndex(index)], this.heap[index]] = [ + this.heap[index], + this.heap[this.getParentIndex(index)], + ]; + + index = this.getParentIndex(index); + } + } + + // Heapify down to maintain the heap property after removing the root + private heapifyDown(): void { + 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]) break; + + [this.heap[index], this.heap[smallerChildIndex]] = [ + this.heap[smallerChildIndex], + this.heap[index], + ]; + + index = smallerChildIndex; + } + } + } + + const heap = new MinHeap(); + + for (const num of nums) { + heap.add(num); + + if (heap.size() > k) { + heap.poll(); + } + } + + return heap.peek() || 0; +}; + +/** + * Accepted + */ +export const findKthLargest2: findKthLargest = (nums, k) => { + nums.sort((a, b) => b - a); + return nums[k - 1]; +};