A café's till shows two live numbers: how many of a given drink were ordered recently, and how many different drinks were.
Build DrinkCounter(window):
order(t, drink) -> None: one order ofdrinkat integer secondt.count(t, drink) -> int: orders ofdrinkin the window ending att.variety(t) -> int: distinct drinks with at least one order in that window.
The window ending at t holds orders with t - window < time <= t. t never decreases across all calls, and many orders can share a second.
c = DrinkCounter(60)
c.order(0, "latte")
c.order(10, "mocha")
c.order(30, "latte")
c.count(30, "latte") # 2
c.variety(30) # 2
c.count(60, "latte") # 1 (the order at 0 is 60 seconds old: gone)
c.variety(75) # 1 (only the latte at 30 is left)
c.count(75, "tea") # 0
c.variety(90) # 0
⭐ Bonus: rescanning the history still passes; a speed test with hundreds of thousands of calls earns a star if every call is O(1) amortized, meaning O(1) on average over many calls (no looping over all drinks in variety).
Show hint
a deque of (t, drink) plus a dict drink -> count for the window. Before each call, pop expired orders and decrement, deleting drinks that reach 0; variety is len(dict).