javascript解决最近通话次数

Number of Recent Calls is one of the leetcode questions that tripped us up. (As you can see how many downvotes it has.🧐) In the following blog, I’ll try to explain the problem and the example line by line to make it easier to understand.

最近通话数量是使我们绊倒的leetcode问题之一。 (如您所见,它有多少票。)在下面的博客中,我将逐行解释问题和示例,以使其更易于理解。

Explanation of the question

问题说明

Write a class RecentCounter to count recent requests.

编写一个RecentCounter来计数最近的请求。

The key point above is class, we need to implement a class, not a function.

上面的重点是类,我们需要实现一个类,而不是一个函数。

It has only one method: ping(int t), where t represents some time in milliseconds.

它只有一种方法: ping(int t) ,其中t表示某个时间(以毫秒为单位)。

The class instance needs to have ping method, which is a function that takes t, an integer in the range of 1 <= t <= 10^9 , as an argument.

类的实例需要具有的方法,它是一个函数,它,整数的范围1 <= t <= 10^9 ,作为一个参数。

Return the number of pings that have been made from 3000 milliseconds ago until now.

返回从3000毫秒之前到现在的ping数量。

The ping method return value is an integer.

方法的返回值是一个整数。

Any ping with time in [t - 3000, t] will count, including the current ping.

时间[t - 3000, t]任何ping都将计数,包括当前ping。

Any previous t that has been called, which is in the range of [current t-3000, current t], will be counted.

在[当前t -3000,当前t ]范围内的任何先前调用的t将被计数。

It is guaranteed that every call to ping uses a strictly larger value of t than before.

可以确保每次对ping调用都严格使用比以前更大的t值。

Each call of ping, the argument t increases, it forms an ascending integer list.

每次调用ping时,参数t都会增加,它形成一个递增的整数列表。

Example 1:

范例1:

Input: inputs = ["RecentCounter","ping","ping","ping","ping"], 
/*
first, we create a new RecentCounter object
obj = new RecentCounter()
then, we call ping four times with four different t */inputs = [[],[1],[100],[3001],[3002]]
/*
obj.ping(1)
obj.ping(100)
obj.ping(3001)
obj.ping(3002)
*/Output: [null,1,2,3,3]/*
object initialized, ping is not called, no return value, nullobj.ping(1) should return 1
obj.ping(100) should return 2
obj.ping(3001) should return 3
obj.ping(3002) should return 3
*/

Note:

注意:

  1. Each test case will have at most 10000 calls to ping.

    每个测试用例最多可以有10000 ping调用。

  2. Each test case will call ping with strictly increasing values of t.

    每个测试用例都将使用t严格增加的值调用ping

  3. Each call to ping will have 1 <= t <= 10^9.

    每次对ping的调用将具有1 <= t <= 10^9

Explanation of the solution

解决方案说明

Let’s start with the easiest, create a class, with a ping method.

让我们从最简单的方法开始,使用ping方法创建一个类。

class RecentCounter {


   ping(t){
   
   }


}

Now we have the frame. According to the question and the example, we need to have a variable that store the t, since it happens multiple times, when we initialize the object, it’s better to have a data type, like an array.

现在我们有了框架。 根据问题和示例,我们需要有一个存储t的变量因为它会多次发生,所以在初始化对象时,最好使用数据类型,例如数组。

class RecentCounter{


     constructor(){
        this.array = []
     }


  ping(t){
  
  
  }
}

How do we use this array? We need to store only the t, that occurs 3000 milliseconds ago until now, which means the previous stored t should be greater or equal to (current t-3000). Since each test case will call ping with strictly increasing values of t, the stored elements in the array is in ascending order. We add the new t to the end and maintain the front of the array within the time frame of t-3000, when it’s not satisfied, we remove the front element. Does sound familiar? First in first out? Yup, it is the queue. After we filtered out the elements which are not in the range, we return the size of the queue.

我们如何使用这个数组? 我们只需要存储T,发生3000毫秒前到现在为止,这意味着上次存储的T应当大于或等于(电流T -3000)。 由于每个测试用例都会使用t的严格增加的值调用ping ,因此数组中存储的元素按升序排列。 我们将新的t添加到末尾,并在t-3000的时间范围内保持数组的前部,如果不满意,则删除前部元素。 听起来很熟悉吗? 先进先出? 是的,这是队列。 过滤掉不在范围内的元素后,我们返回队列的大小。

ping(1)
queue = [ 1 ] // 1ping(100)
queue = [ 1, 100 ] // 2ping(3001)
queue = [ 1, 100, 3001 ] // 3ping(3002)
queue = [ 100, 3001, 3002 ] // 3

Let’s implement the code.

让我们实现代码。

class RecentCounter{
    
    constructor(){
        this.queue = []
    }
    
    ping(t){
      //add the current t to the end 
        this.queue.push(t)
       // if the previous t is not in the range
        while(this.queue[0] < t - 3000){
        // we remove from the beginning    
          this.queue.shift()
        }
        
        //return the size of the queue
        return this.queue.length
    }
}

The time complexity of ping method is O(n).

ping方法的时间复杂度为O(n)

I hope this could be helpful to solve the similar problems.

我希望这将有助于解决类似的问题。

翻译自: https://medium.com/javascript-in-plain-english/javascript-solution-to-number-of-recent-calls-f979835173e3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值