By Array


  • 0
    X
    private final int[][] hits = new int[300][2];
    
    /** Record a hit.
    @param timestamp - The current timestamp (in seconds granularity). */
    public void hit(int timestamp) {
        final int idx = timestamp % 300;
    
        if (hits[idx][0] != timestamp) {
            hits[idx][0] = timestamp;
            hits[idx][1] = 1;
        } else {
            hits[idx][1]++;
        }
    }
    
    /** Return the number of hits in the past 5 minutes.
    @param timestamp - The current timestamp (in seconds granularity). */
    public int getHits(int timestamp) {
        return Arrays.stream(hits).filter(e -> timestamp - e[0] < 300).mapToInt(e -> e[1]).sum();
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.