Skip to content

Commit

Permalink
211th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Sep 29, 2024
1 parent c23e164 commit 0f5793c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,15 @@ 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][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 |
| Heap / Priority Queue | | |
| ------------------------------------- | ---------------- | ------ |
| 215. Kth Largest Element in an Array | [Solution][215] | Medium |
| 2336. Smallest Number in Infinite Set | [Solution][2336] | 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
[2336]: ./src/page-22/2336.%20Smallest%20Number%20in%20Infinite%20Set/SmallestInfiniteSet.ts

| Binary Search | | |
| -------------------------------------------- | -------- | ------ |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { SmallestInfiniteSet } from './SmallestInfiniteSet';

describe('2336. Smallest Number in Infinite Set', () => {
test('SmallestInfiniteSet', () => {
const smallestInfiniteSet = new SmallestInfiniteSet();
smallestInfiniteSet.addBack(2);
expect(smallestInfiniteSet.popSmallest()).toBe(1);
expect(smallestInfiniteSet.popSmallest()).toBe(2);
expect(smallestInfiniteSet.popSmallest()).toBe(3);
smallestInfiniteSet.addBack(1);
expect(smallestInfiniteSet.popSmallest()).toBe(1);
expect(smallestInfiniteSet.popSmallest()).toBe(4);
expect(smallestInfiniteSet.popSmallest()).toBe(5);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Accepted
*/
export class SmallestInfiniteSet {
private minHeap: number[];
private addedBack: Set<number>;
private current: number;

constructor() {
this.minHeap = [];
this.addedBack = new Set();
this.current = 1;
}

popSmallest(): number {
if (this.minHeap.length > 0) {
const smallest = this.minHeap.shift();

if (smallest !== undefined) {
this.addedBack.delete(smallest);
return smallest;
}
}

// If heap is empty, return the current smallest number in sequence
const smallest = this.current;
this.current += 1; // Manually increment current
return smallest;
}

addBack(num: number): void {
if (num < this.current && !this.addedBack.has(num)) {
this.minHeap.push(num);
this.minHeap.sort((a, b) => a - b); // Ensure heap property
this.addedBack.add(num);
}
}
}

0 comments on commit 0f5793c

Please sign in to comment.