Skip to content

Commit

Permalink
210th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Sep 29, 2024
1 parent 8fa3bc8 commit c23e164
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 6 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | |
| -------------------------------------------- | -------- | ------ |
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
});
118 changes: 118 additions & 0 deletions src/page-2/215. Kth Largest Element in an Array/findKthLargest.ts
Original file line number Diff line number Diff line change
@@ -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];
};

0 comments on commit c23e164

Please sign in to comment.