|
| 1 | +package com.leetcode.design; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +/** |
| 6 | + * Level: Medium |
| 7 | + * Problem Link: https://leetcode.com/problems/design-hit-counter/ |
| 8 | + * Problem Description: |
| 9 | + * Design a hit counter which counts the number of hits received in the past 5 minutes. |
| 10 | + * |
| 11 | + * Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made |
| 12 | + * to the system in chronological order (ie, the timestamp is monotonically increasing). You may assume that the |
| 13 | + * earliest timestamp starts at 1. |
| 14 | + * |
| 15 | + * It is possible that several hits arrive roughly at the same time. |
| 16 | + * |
| 17 | + * Example: |
| 18 | + * |
| 19 | + * HitCounter counter = new HitCounter(); |
| 20 | + * |
| 21 | + * // hit at timestamp 1. |
| 22 | + * counter.hit(1); |
| 23 | + * |
| 24 | + * // hit at timestamp 2. |
| 25 | + * counter.hit(2); |
| 26 | + * |
| 27 | + * // hit at timestamp 3. |
| 28 | + * counter.hit(3); |
| 29 | + * |
| 30 | + * // get hits at timestamp 4, should return 3. |
| 31 | + * counter.getHits(4); |
| 32 | + * |
| 33 | + * // hit at timestamp 300. |
| 34 | + * counter.hit(300); |
| 35 | + * |
| 36 | + * // get hits at timestamp 300, should return 4. |
| 37 | + * counter.getHits(300); |
| 38 | + * |
| 39 | + * // get hits at timestamp 301, should return 3. |
| 40 | + * counter.getHits(301); |
| 41 | + * |
| 42 | + * Follow up: |
| 43 | + * What if the number of hits per second could be very large? Does your design scale? |
| 44 | + * |
| 45 | + * Runtime: <a href="https://leetcode.com/submissions/detail/248917167/">42 ms</a> (better than ~98%). |
| 46 | + * |
| 47 | + * @author rampatra |
| 48 | + * @since 2019-08-04 |
| 49 | + */ |
| 50 | +public class DesignHitCounter { |
| 51 | + |
| 52 | + private int[] timestamps; |
| 53 | + private int[] hits; |
| 54 | + |
| 55 | + /** |
| 56 | + * Initialize your data structure here. |
| 57 | + */ |
| 58 | + public DesignHitCounter() { |
| 59 | + this.timestamps = new int[300]; |
| 60 | + this.hits = new int[300]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Record a hit. |
| 65 | + * |
| 66 | + * @param timestamp - The current timestamp (in seconds granularity). |
| 67 | + */ |
| 68 | + public void hit(int timestamp) { |
| 69 | + int bucket = timestamp % 300; |
| 70 | + if (timestamps[bucket] == timestamp) { |
| 71 | + hits[bucket]++; |
| 72 | + } else { |
| 73 | + timestamps[bucket] = timestamp; |
| 74 | + hits[bucket] = 1; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Return the number of hits in the past 5 minutes. |
| 80 | + * |
| 81 | + * @param timestamp - The current timestamp (in seconds granularity). |
| 82 | + */ |
| 83 | + public int getHits(int timestamp) { |
| 84 | + int totalHits = 0; |
| 85 | + for (int i = 0; i < 300; i++) { |
| 86 | + if (timestamp - 300 < timestamps[i]) { |
| 87 | + totalHits += hits[i]; |
| 88 | + } |
| 89 | + } |
| 90 | + return totalHits; |
| 91 | + } |
| 92 | + |
| 93 | + public static void main(String[] args) { |
| 94 | + DesignHitCounter counter = new DesignHitCounter(); |
| 95 | + |
| 96 | + // hit at timestamp 1. |
| 97 | + counter.hit(1); |
| 98 | + |
| 99 | + // hit at timestamp 2. |
| 100 | + counter.hit(2); |
| 101 | + |
| 102 | + // hit at timestamp 3. |
| 103 | + counter.hit(3); |
| 104 | + |
| 105 | + // get hits at timestamp 4, should return 3. |
| 106 | + assertEquals(3, counter.getHits(4)); |
| 107 | + |
| 108 | + // hit at timestamp 300. |
| 109 | + counter.hit(300); |
| 110 | + |
| 111 | + // get hits at timestamp 300, should return 4. |
| 112 | + assertEquals(4, counter.getHits(300)); |
| 113 | + |
| 114 | + // get hits at timestamp 301, should return 3. |
| 115 | + assertEquals(3, counter.getHits(301)); |
| 116 | + } |
| 117 | +} |
0 commit comments