Create a programming task related to algorithms and data structures,…
- User
Assistant: User: Create a programming task related to algorithms and data structures, similar to LeetCode tasks. Follow this scenario: [Title]: [Task description]: should involve processing a dataset (e.g., an array of numbers) where a specific statistical measure (average, median, mode, etc.) needs to be calculated. [Input]: A collection of numerical values. [Output]: A statistical measure calculated from the collection. [Examples]:Provide several examples of input and expected output with explanations. [Function signature]: [Solving]:Propose a function with a typical signature that will solve the task.
Assistant:
- Assistant
[Title]: Median of a Stream
[Task description]: Given a continuous stream of integers, design a data structure that supports adding a new number to the stream of input and finding the current median of the stream. The median is the middle value in an ordered integer list. If the size of the list is even, the median is the average of the two middle values.
[Input]: A series of integers added one-by-one into the stream.
[Output]: After each insertion, provide the median of the current numbers in the stream.
[Examples]:
- After adding 1 to the stream, median is 1.
- After adding 2 to the stream, median is (1+2)/2 = 1.5.
- After adding 3 to the stream, median is 2.
- After adding another 1 to the stream, median is 1.5.
[Function signature]:
class MedianFinder: def __init__(self): # Initializes the MedianFinder object. pass def addNum(self, num: int) -> None: # Adds a number into the data structure. pass def findMedian(self) -> float: # Returns the median of all elements so far. pass