Similar Problems

Similar Problems not available

Smallest Number In Infinite Set - Leetcode Solution

Companies:

LeetCode:  Smallest Number In Infinite Set Leetcode Solution

Difficulty: Medium

Topics: design heap-priority-queue hash-table  

Problem Statement:

You are given a function reader() that reads an integer from an infinite stream of integers, and a function smallestNumber() that returns the smallest number read so far from the stream.

Implement smallestNumber() to find the smallest number in the infinite stream of integers read by reader().

Solution:

To solve this problem, we need to constantly keep track of the smallest number we have seen so far in the input stream. We will keep a variable that will store the current minimum value in the stream.

In the first step, we initialize the current minimum variable with the first number from the stream. We can obtain the first number from the stream using the reader() function.

Next, we will enter a loop that runs infinitely, reading the next integer from the input stream one by one. For each new integer we read:

  1. We compare it with the current minimum value we have stored, and update the minimum value if the new value read is smaller than the current minimum value.
  2. We return the updated minimum value.

Whenever a new number is read from the stream, our function smallestNumber() will compare it with the currently stored minimum value, and update the current minimum if the new number is smaller. By storing and updating the minimum value in real-time, we can always return the smallest number read so far.

Here's the code for the solution:

class Solution:
    def __init__(self):
        self.min_val = float('inf')
        
    def smallestNumber(self) -> int:
        return self.min_val
    
    def reader(self):
        # returns the next integer from the input stream
        # this function is already implemented and provided by leetcode
        pass
        
    def read_min(self):
        while True:
            num = self.reader()
            self.min_val = min(self.min_val, num)

We initialize the min_val variable to infinity in the constructor and provide the implementation for the smallestNumber function to return the current minimum value. The read_min function is the heart of our solution - it reads integers from the input stream one by one and updates the minimum value as it goes.

The loop inside the read_min function runs forever, reading the next integer from the input stream and updating the minimum value if it is smaller than the current minimum. Since the stream is infinite, the loop is never terminated. In practice, we can break out of the loop after reading a sufficient number of integers from the input stream.

To use our solution, we create an instance of the class Solution and call its read_min function to read integers from the stream in real-time and update the current minimum value. Once we have read all the integers we want, we can call the smallestNumber function to get the smallest number read so far.

sol = Solution()
sol.read_min() # reads integers from stream and updates current minimum
min_num = sol.smallestNumber() # gets the smallest number read so far

Time Complexity Analysis:

The time complexity of our solution is O(n), where n is the total number of integers we read from the input stream. The read_min function reads each integer once and updates the minimum value in constant time, so the overall time complexity is linear in the length of the input stream.

Space Complexity Analysis:

The space complexity of our solution is O(1) - we only need to store a single integer (min_val) to keep track of the current minimum value. No additional data structures are required, so the space complexity is constant.

Smallest Number In Infinite Set Solution Code

1