Skip to content

Commit d66dcc4

Browse files
committed
feat: add solutions to lc problem: No.0933
No.0933.Number of Recent Calls
1 parent 9659c6f commit d66dcc4

File tree

2 files changed

+220
-11
lines changed

2 files changed

+220
-11
lines changed

solution/0900-0999/0933.Number of Recent Calls/README.md

Lines changed: 114 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ recentCounter.ping(3002); // requests = [1, <strong>100</strong>, <strong>3001<
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53-
**方法一:暴力**
54-
55-
使用数组存储所有请求记录,当请求发生时,将记录存入数组,然后遍历数组,统计其中满足 `[t - 3000, t]` 的元素数量,返回即可。
56-
57-
**方法二:队列**
53+
**方法一:队列**
5854

5955
由题得知,`t`**严格递增**的,当一个元素不满足 `[t - 3000, t]` 条件时,在后续的请求当中,它也不可能满足。
6056

6157
对此,需要将其从记录容器中移除,减少无意义的比较。
6258

6359
可以使用队列。每次将 `t` 进入队尾,同时从队头开始,依次移除小于 `t - 3000` 的元素。然后返回队列的大小(`q.size()`)即可。
6460

61+
**方法二:二分查找**
62+
63+
`t` 严格单调递增,非常适合用二分查找来定位 `[t-3000, t]` 的左右边界。
64+
6565
<!-- tabs:start -->
6666

6767
### **Python3**
@@ -81,6 +81,22 @@ class RecentCounter:
8181
return len(self.q)
8282

8383

84+
# Your RecentCounter object will be instantiated and called as such:
85+
# obj = RecentCounter()
86+
# param_1 = obj.ping(t)
87+
```
88+
89+
```python
90+
class RecentCounter:
91+
92+
def __init__(self):
93+
self.s = []
94+
95+
def ping(self, t: int) -> int:
96+
self.s.append(t)
97+
return bisect_left(self.s, t + 1) - bisect_left(self.s, t - 3000)
98+
99+
84100
# Your RecentCounter object will be instantiated and called as such:
85101
# obj = RecentCounter()
86102
# param_1 = obj.ping(t)
@@ -94,10 +110,8 @@ class RecentCounter:
94110
class RecentCounter {
95111
private Deque<Integer> q = new ArrayDeque<>();
96112

97-
public RecentCounter() {
98-
99-
}
100-
113+
public RecentCounter() {}
114+
101115
public int ping(int t) {
102116
q.offer(t);
103117
while (q.peekFirst() < t - 3000) {
@@ -106,6 +120,40 @@ class RecentCounter {
106120
return q.size();
107121
}
108122
}
123+
/**
124+
* Your RecentCounter object will be instantiated and called as such:
125+
* RecentCounter obj = new RecentCounter();
126+
* int param_1 = obj.ping(t);
127+
*/
128+
```
129+
130+
```java
131+
class RecentCounter {
132+
private int[] s = new int[10010];
133+
private int idx;
134+
135+
public RecentCounter() {
136+
137+
}
138+
139+
public int ping(int t) {
140+
s[idx++] = t;
141+
return search(t + 1) - search(t - 3000);
142+
}
143+
144+
private int search(int x) {
145+
int left = 0, right = idx;
146+
while (left < right) {
147+
int mid = (left + right) >> 1;
148+
if (s[mid] >= x) {
149+
right = mid;
150+
} else {
151+
left = mid + 1;
152+
}
153+
}
154+
return left;
155+
}
156+
}
109157

110158
/**
111159
* Your RecentCounter object will be instantiated and called as such:
@@ -124,7 +172,7 @@ public:
124172
RecentCounter() {
125173

126174
}
127-
175+
128176
int ping(int t) {
129177
q.push(t);
130178
while (q.front() < t - 3000) q.pop();
@@ -139,6 +187,28 @@ public:
139187
*/
140188
```
141189
190+
```cpp
191+
class RecentCounter {
192+
public:
193+
vector<int> s;
194+
195+
RecentCounter() {
196+
197+
}
198+
199+
int ping(int t) {
200+
s.push_back(t);
201+
return lower_bound(s.begin(), s.end(), t + 1) - lower_bound(s.begin(), s.end(), t - 3000);
202+
}
203+
};
204+
205+
/**
206+
* Your RecentCounter object will be instantiated and called as such:
207+
* RecentCounter* obj = new RecentCounter();
208+
* int param_1 = obj->ping(t);
209+
*/
210+
```
211+
142212
### **Go**
143213

144214
```go
@@ -165,6 +235,39 @@ func (this *RecentCounter) Ping(t int) int {
165235
*/
166236
```
167237

238+
```go
239+
type RecentCounter struct {
240+
s []int
241+
}
242+
243+
func Constructor() RecentCounter {
244+
return RecentCounter{[]int{}}
245+
}
246+
247+
func (this *RecentCounter) Ping(t int) int {
248+
this.s = append(this.s, t)
249+
search := func(x int) int {
250+
left, right := 0, len(this.s)
251+
for left < right {
252+
mid := (left + right) >> 1
253+
if this.s[mid] >= x {
254+
right = mid
255+
} else {
256+
left = mid + 1
257+
}
258+
}
259+
return left
260+
}
261+
return search(t+1) - search(t-3000)
262+
}
263+
264+
/**
265+
* Your RecentCounter object will be instantiated and called as such:
266+
* obj := Constructor();
267+
* param_1 := obj.Ping(t);
268+
*/
269+
```
270+
168271
### **JavaScript**
169272

170273
```js
@@ -200,7 +303,7 @@ public class RecentCounter {
200303
public RecentCounter() {
201304

202305
}
203-
306+
204307
public int Ping(int t) {
205308
q.Enqueue(t);
206309
while (q.Peek() < t - 3000)

solution/0900-0999/0933.Number of Recent Calls/README_EN.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ class RecentCounter:
6161
return len(self.q)
6262

6363

64+
# Your RecentCounter object will be instantiated and called as such:
65+
# obj = RecentCounter()
66+
# param_1 = obj.ping(t)
67+
```
68+
69+
```python
70+
class RecentCounter:
71+
72+
def __init__(self):
73+
self.s = []
74+
75+
def ping(self, t: int) -> int:
76+
self.s.append(t)
77+
return bisect_left(self.s, t + 1) - bisect_left(self.s, t - 3000)
78+
79+
6480
# Your RecentCounter object will be instantiated and called as such:
6581
# obj = RecentCounter()
6682
# param_1 = obj.ping(t)
@@ -92,6 +108,41 @@ class RecentCounter {
92108
*/
93109
```
94110

111+
```java
112+
class RecentCounter {
113+
private int[] s = new int[10010];
114+
private int idx;
115+
116+
public RecentCounter() {
117+
118+
}
119+
120+
public int ping(int t) {
121+
s[idx++] = t;
122+
return search(t + 1) - search(t - 3000);
123+
}
124+
125+
private int search(int x) {
126+
int left = 0, right = idx;
127+
while (left < right) {
128+
int mid = (left + right) >> 1;
129+
if (s[mid] >= x) {
130+
right = mid;
131+
} else {
132+
left = mid + 1;
133+
}
134+
}
135+
return left;
136+
}
137+
}
138+
139+
/**
140+
* Your RecentCounter object will be instantiated and called as such:
141+
* RecentCounter obj = new RecentCounter();
142+
* int param_1 = obj.ping(t);
143+
*/
144+
```
145+
95146
### **C++**
96147

97148
```cpp
@@ -117,6 +168,28 @@ public:
117168
*/
118169
```
119170
171+
```cpp
172+
class RecentCounter {
173+
public:
174+
vector<int> s;
175+
176+
RecentCounter() {
177+
178+
}
179+
180+
int ping(int t) {
181+
s.push_back(t);
182+
return lower_bound(s.begin(), s.end(), t + 1) - lower_bound(s.begin(), s.end(), t - 3000);
183+
}
184+
};
185+
186+
/**
187+
* Your RecentCounter object will be instantiated and called as such:
188+
* RecentCounter* obj = new RecentCounter();
189+
* int param_1 = obj->ping(t);
190+
*/
191+
```
192+
120193
### **Go**
121194

122195
```go
@@ -143,6 +216,39 @@ func (this *RecentCounter) Ping(t int) int {
143216
*/
144217
```
145218

219+
```go
220+
type RecentCounter struct {
221+
s []int
222+
}
223+
224+
func Constructor() RecentCounter {
225+
return RecentCounter{[]int{}}
226+
}
227+
228+
func (this *RecentCounter) Ping(t int) int {
229+
this.s = append(this.s, t)
230+
search := func(x int) int {
231+
left, right := 0, len(this.s)
232+
for left < right {
233+
mid := (left + right) >> 1
234+
if this.s[mid] >= x {
235+
right = mid
236+
} else {
237+
left = mid + 1
238+
}
239+
}
240+
return left
241+
}
242+
return search(t+1) - search(t-3000)
243+
}
244+
245+
/**
246+
* Your RecentCounter object will be instantiated and called as such:
247+
* obj := Constructor();
248+
* param_1 := obj.Ping(t);
249+
*/
250+
```
251+
146252
### **JavaScript**
147253

148254
```js

0 commit comments

Comments
 (0)