Leetcode PHP solution — D50 933. Number of recent calls

Time:2020-1-11

D50 933. Number of Recent Calls

Title Link

933. Number of Recent Calls

Title Analysis

I was blinded by the truth of the subject.

Returns the number of pings (including the current pings) since 3000 milliseconds.

transferpingFunction, the arguments passed intIs the number of milliseconds currently Ping.

thinking

In fact, the number of pings in the first 3000 milliseconds is returned.

Save the milliseconds of each Ping, and then go back to find the Ping within 3000 milliseconds.

That is, add 1 to the current number of Ping until the current number of milliseconds minus the previous number of Ping milliseconds is greater than 3000.

Final code

<?php
class RecentCounter {
    public $pings = [];
    public $head = 0;

    function __construct() {
        
    }
    function ping($t) {
        if(is_null($t)){
            return null;
        }
        $this->pings[] = $t;
        
        while(($this->pings[count($this->pings)-1]-$this->pings[$this->head])>3000){
            $this->head++;
        }
        return count($this->pings)-$this->head;
    }
}
/**
 * Your RecentCounter object will be instantiated and called as such:
 * $obj = RecentCounter();
 * $ret_1 = $obj->ping($t);
 */ 

If you think this article is useful to you, you are welcome to use AI power generation support.

Recommended Today

Array of algorithms — sum of three numbers

Sum of three numbers difficultysecondary Here is an array of N integersnums, judgmentnumsAre there three elements a, B, C in a such that a + B + C = 0? Please find all triples that satisfy the condition and do not repeat.be careful:The answer cannot contain duplicate triples. Example:Given array nums = [- 1, 0, […]