Design a class StreakCounter that receives one value at a time. When a value x arrives, its streak is the number of consecutive values, counting backwards from x itself, that are less than or equal to x (so the streak is at least 1).
StreakCounter(): create an empty counter.add(x): recordxand return its streak.
Examples
Input: ops = ["StreakCounter", "add", "add", "add", "add", "add", "add", "add"]
args = [[], [50], [40], [45], [45], [30], [48], [60]]
Output: [None, 1, 1, 2, 3, 1, 5, 7]
Explanation: the second 45 counts the first one too, because equal values continue a streak.
48 is preceded by 30, 45, 45, 40 (all <= 48) and then 50 stops the streak, so its streak is 5.
Constraints
- At most
10**4calls toadd - Target: amortised
O(1)per call (rescanning previous values is too slow)
Goals
- Collapse dominated entries into a single stack element with a combined count
- Achieve amortised O(1) per call on a streaming input