Payments app~40 min2s · 256 MBtarget O(n) time, O(n) spacequeues
A card platform protects itself from stolen cards and runaway scripts. Each card may be swiped only a few times in a short period, and the platform as a whole accepts only so many swipes in that period.
The check must answer in constant time, because it runs on every swipe. A swipe that is blocked is rejected and leaves no trace.
Write run_limiter(limit, global_limit, window, commands, args).
commands[i] is "swipe" or "count". Its arguments args[i] are [card, time]. The times never go down.t0 is recent at time t when t - t0 < window."swipe": accept the swipe when the card has fewer than limit recent accepted swipes and all cards together have fewer than global_limit. Answer 1 if accepted, and remember it. Otherwise answer 0 and remember nothing."count": answer the number of recent accepted swipes of that card. It changes nothing.Input: limit = 2, global_limit = 3, window = 10, commands = ["swipe", "swipe", "swipe", "count", "swipe", "swipe", "swipe", "count"], args = [[7, 0], [7, 4], [7, 8], [7, 9], [8, 9], [9, 10], [8, 11], [7, 14]]
Output: [1, 1, 0, 2, 1, 1, 0, 0]The trace shows the card's accepted times and the number of accepted swipes of everyone that are recent.
| i | Command | What happens | Card's times after | Everyone | Answer |
|---|---|---|---|---|---|
| 0 | swipe 7 at 0 | accepted | [0] | 1 | 1 |
| 1 | swipe 7 at 4 | accepted | [0, 4] | 2 | 1 |
| 2 | swipe 7 at 8 | blocked by the card limit | [0, 4] | 2 | 0 |
| 3 | count 7 at 9 | count of card 7 | [0, 4] | 2 | 2 |
| 4 | swipe 8 at 9 | accepted | [9] | 3 | 1 |
| 5 | swipe 9 at 10 | accepted | [10] | 3 | 1 |
| 6 | swipe 8 at 11 | blocked by the global limit | [9] | 3 | 0 |
| 7 | count 7 at 14 | count of card 7 | [] | 2 | 0 |
The swipe at i = 2 is blocked by the card limit, and the swipe at i = 6 by the global limit. By time 14, both swipes of card 7 are 10 seconds old or older, so the last count is 0.
Input: limit = 1, global_limit = 5, window = 5, commands = ["swipe", "swipe", "swipe", "swipe"], args = [[1, 0], [1, 4], [1, 5], [2, 5]]
Output: [1, 0, 1, 1]Card 1 swipes at time 0 and is accepted. At time 4 it is blocked, because the first swipe is still recent. At time 5 the first swipe is exactly 5 seconds old, so it has expired, and the swipe is accepted. Card 2 is accepted.
Input: limit = 3, global_limit = 1, window = 100, commands = ["swipe", "swipe", "count", "count"], args = [[4, 10], [5, 20], [4, 30], [5, 30]]
Output: [1, 0, 1, 0]The global limit is 1, so the swipe of card 5 is blocked even though card 5 has swiped nothing. The counts are 1 for card 4 and 0 for card 5.
1 ≤ limit, global_limit ≤ 100,000; 1 ≤ window ≤ 86,400 (seconds)1 ≤ commands.length ≤ 100,000; commands[i] is "swipe" or "count"; args has the same length, and each row has 2 numbers0 ≤ card ≤ 1,000,000,000 (card ids); 0 ≤ time ≤ 2,678,400 (seconds), never going downScanning every accepted swipe on every command costs about n × n / 2 steps, about 5 × 10^9 at n = 100,000. Times never go down, so the oldest swipe is always the first to expire. A queue of accepted times, for each card and for everyone, removes expired swipes from the front in total O(n) steps.