Practice/Meta/Leetcode 901. Online Stock Span
CodingOptional
Design a data structure that processes a stream of product prices in real-time. For each incoming price, you need to calculate how many consecutive recent prices (including the current one) were less than or equal to this price.
You'll implement a class StreamAnalyzer with the following methods:
The span is defined as the maximum number of consecutive days ending with today where each day's price was less than or equal to today's price.
StreamAnalyzer class with an __init__ method and a record_price methodrecord_price method must return an integer representing the spanrecord_price ≤ 10,000record_price callExample 1:
analyzer = StreamAnalyzer() analyzer.record_price(100) # Returns 1 analyzer.record_price(80) # Returns 1 analyzer.record_price(60) # Returns 1 analyzer.record_price(70) # Returns 2 (days with prices [60, 70] are ≤ 70) analyzer.record_price(60) # Returns 1 analyzer.record_price(75) # Returns 4 (days with prices [60, 70, 60, 75] are ≤ 75) analyzer.record_price(85) # Returns 6 (days with prices [80, 60, 70, 60, 75, 85] are ≤ 85)
Example 2:
analyzer = StreamAnalyzer() analyzer.record_price(10) # Returns 1 analyzer.record_price(20) # Returns 2 analyzer.record_price(30) # Returns 3
Example 3:
analyzer = StreamAnalyzer() analyzer.record_price(50) # Returns 1 analyzer.record_price(40) # Returns 1 analyzer.record_price(30) # Returns 1