From 0f5793c0f300eb12b9c22849dd77b8c29542d77a Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sun, 29 Sep 2024 20:21:55 +0800 Subject: [PATCH] 211th Commit --- README.md | 13 ++++--- .../SmallestInfiniteSet.test.ts | 15 ++++++++ .../SmallestInfiniteSet.ts | 38 +++++++++++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 src/page-22/2336. Smallest Number in Infinite Set/SmallestInfiniteSet.test.ts create mode 100644 src/page-22/2336. Smallest Number in Infinite Set/SmallestInfiniteSet.ts diff --git a/README.md b/README.md index 7099ea4..b7fc5e0 100644 --- a/README.md +++ b/README.md @@ -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 | | | | -------------------------------------------- | -------- | ------ | diff --git a/src/page-22/2336. Smallest Number in Infinite Set/SmallestInfiniteSet.test.ts b/src/page-22/2336. Smallest Number in Infinite Set/SmallestInfiniteSet.test.ts new file mode 100644 index 0000000..8f1adf3 --- /dev/null +++ b/src/page-22/2336. Smallest Number in Infinite Set/SmallestInfiniteSet.test.ts @@ -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); + }); +}); diff --git a/src/page-22/2336. Smallest Number in Infinite Set/SmallestInfiniteSet.ts b/src/page-22/2336. Smallest Number in Infinite Set/SmallestInfiniteSet.ts new file mode 100644 index 0000000..b01fe40 --- /dev/null +++ b/src/page-22/2336. Smallest Number in Infinite Set/SmallestInfiniteSet.ts @@ -0,0 +1,38 @@ +/** + * Accepted + */ +export class SmallestInfiniteSet { + private minHeap: number[]; + private addedBack: Set; + 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); + } + } +}