-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c23e164
commit 0f5793c
Showing
3 changed files
with
60 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/page-22/2336. Smallest Number in Infinite Set/SmallestInfiniteSet.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
src/page-22/2336. Smallest Number in Infinite Set/SmallestInfiniteSet.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |