beat 81% JAVA solution use ArrayList


  • 0
    V
    
    public class HitCounter {
        List<Integer> q;
        /** Initialize your data structure here. */
        public HitCounter() {
            q = new ArrayList<Integer>();
        }
        
        /** Record a hit.
            @param timestamp - The current timestamp (in seconds granularity). */
        public void hit(int timestamp) {
            q.add(timestamp);
        }
        
        /** Return the number of hits in the past 5 minutes.
            @param timestamp - The current timestamp (in seconds granularity). */
        public int getHits(int timestamp) {
            int n = 0;
            while(n < q.size() && timestamp - q.get(n) >= 300) {
                n++;
            }
           
            q = q.subList(n, q.size());
            return q.size();
        }
    }
    
    /**
     * Your HitCounter object will be instantiated and called as such:
     * HitCounter obj = new HitCounter();
     * obj.hit(timestamp);
     * int param_2 = obj.getHits(timestamp);
     */
    
    

Log in to reply
 

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