Solving Leetcode Interviews in Seconds with AI: Smallest Number in Infinite Set
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2336" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You have a set which contains all positive integers [1, 2, 3, 4, 5, ...]. Implement the SmallestInfiniteSet class: SmallestInfiniteSet() Initializes the SmallestInfiniteSet object to contain all positive integers. int popSmallest() Removes and returns the smallest integer contained in the infinite set. void addBack(int num) Adds a positive integer num back into the infinite set, if it is not already in the infinite set. Example 1: Input ["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"] [[], [2], [], [], [], [1], [], [], []] Output [null, null, 1, 2, 3, null, 1, 4, 5] Explanation SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet(); smallestInfiniteSet.addBack(2); // 2 is already in the set, so no change is made. smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set. smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set. smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set. smallestInfiniteSet.addBack(1); // 1 is added back to the set. smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and // is the smallest number, and remove it from the set. smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set. smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set. Constraints: 1 <= num <= 1000 At most 1000 calls will be made in total to popSmallest and addBack.
Explanation
- Maintain a sorted set of added-back numbers: Use a min-heap (priority queue) to efficiently store numbers that have been added back into the set after being popped. This ensures quick access to the smallest available number.
- Track the smallest "missing" number: Keep track of the smallest positive integer that has not yet been popped. This avoids unnecessary heap operations when popping the smallest element in the infinite set.
- Runtime Complexity: O(log K) for
addBackandpopSmallest, where K is the number of calls toaddBack. O(1) for initialization. Storage Complexity: O(K), where K is the number of calls toaddBack.
Code
import heapq
class SmallestInfiniteSet:
def __init__(self):
self.heap = []
self.smallest = 1
def popSmallest(self):
if self.heap:
smallest_added = heapq.heappop(self.heap)
return smallest_added
else:
self.smallest += 1
return self.smallest - 1
def addBack(self, num):
if num < self.smallest and num not in self.heap:
heapq.heappush(self.heap, num)
# Your SmallestInfiniteSet object will be instantiated and called as such:
# obj = SmallestInfiniteSet()
# param_1 = obj.popSmallest()
# obj.addBack(num)