[Swift Weekly Contest 109]LeetCode933. 最近的请求次数 | Number of Recent Calls

Write a class RecentCounter to count recent requests.

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

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

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

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

Example 1:

Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]
Output: [null,1,2,3,3]

Note:

  1. Each test case will have at most 10000 calls to ping.
  2. Each test case will call ping with strictly increasing values of t.
  3. Each call to ping will have 1 <= t <= 10^9.


写一个 RecentCounter 类来计算最近的请求。

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

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

任何处于 [t - 3000, t] 时间范围之内的 ping 都将会被计算在内,包括当前(指 t 时刻)的 ping

保证每次对 ping 的调用都使用比之前更大的 t 值。

示例:

输入:inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]
输出:[null,1,2,3,3]

提示:

  1. 每个测试用例最多调用 10000 次 ping
  2. 每个测试用例会使用严格递增的 t 值来调用 ping
  3. 每次调用 ping 都有 1 <= t <= 10^9


1204ms

 1 class RecentCounter {
 2     var pq:Queue<Int> = Queue<Int>()
 3
 4     init() {
 5
 6     }
 7
 8     func ping(_ t: Int) -> Int {
 9         pq.enQueue(t)
10         while(!pq.isEmpty() && pq.getFirst()! < t-3000)
11         {
12             pq.deQueue()
13         }
14         return pq.count
15     }
16 }
17
18 public struct Queue<T> {
19
20     // 泛型数组:用于存储数据元素
21     fileprivate var queue: [T]
22
23     // 返回队列中元素的个数
24     public var count: Int {
25         return queue.count
26     }
27
28     // 构造函数:创建一个空的队列
29     public init() {
30         queue = [T]()
31     }
32
33     //通过既定数组构造队列
34     init(_ arr:[T]){
35         queue = arr
36     }
37
38     // 如果定义了默认值,则可以在调用函数时省略该参数
39     init(_ elements: T...) {
40         queue = elements
41     }
42
43     // 检查队列是否为空
44     // - returns: 如果队列为空,则返回true,否则返回false
45     public func isEmpty() -> Bool {
46         return queue.isEmpty
47     }
48
49     // 入队列操作:将元素添加到队列的末尾
50     public mutating func enQueue(_ element: T) {
51         queue.append(element)
52     }
53
54     // 出队列操作:删除并返回队列中的第一个元素
55     public mutating func deQueue() -> T? {
56         return queue.removeFirst()
57     }
58
59     // 返回队列中的第一个元素(不删除)
60     public func getFirst() -> T? {
61         return queue.first!
62     }
63 }
64
65 /**
66  * Your RecentCounter object will be instantiated and called as such:
67  * let obj = RecentCounter()
68  * let ret_1: Int = obj.ping(t)
69  */
70  

原文地址:https://www.cnblogs.com/strengthen/p/9903392.html

时间: 2024-10-06 13:53:10

[Swift Weekly Contest 109]LeetCode933. 最近的请求次数 | Number of Recent Calls的相关文章

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the smallest nu

[Swift Weekly Contest 109]LeetCode936. 戳印序列 | Stamping The Sequence

You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the s

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

[Swift Weekly Contest 108]LeetCode932. 漂亮数组 | Beautiful Array

For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such that: For every i < j, there is no k with i < k < j such that A[k] * 2 = A[i] + A[j]. Given N, return any beautiful array A.  (It is guaranteed th

[Swift Weekly Contest 108]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

[Swift Weekly Contest 111]LeetCode941. 有效的山脉数组 | Valid Mountain Array

Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A is a mountain array if and only if: A.length >= 3 There exists some i with 0 < i < A.length - 1 such that: A[0] < A[1] < ... A[i-1] < A[

[Swift Weekly Contest 111]LeetCode944. 删除列以使之有序 | Delete Columns to Make Sorted

We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have a string "abcdef" and dele