Skip to content

Commit 1ffbeab

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

File tree

2 files changed

+201
-13
lines changed

2 files changed

+201
-13
lines changed

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

Lines changed: 95 additions & 13 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 len(self.s) - 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)
@@ -92,18 +108,29 @@ class RecentCounter:
92108

93109
```java
94110
class RecentCounter {
95-
private Deque<Integer> q = new ArrayDeque<>();
111+
private int[] s = new int[10010];
112+
private int idx;
96113

97114
public RecentCounter() {
98-
115+
99116
}
100117

101118
public int ping(int t) {
102-
q.offer(t);
103-
while (q.peekFirst() < t - 3000) {
104-
q.pollFirst();
119+
s[idx++] = t;
120+
return idx - search(t - 3000);
121+
}
122+
123+
private int search(int x) {
124+
int left = 0, right = idx;
125+
while (left < right) {
126+
int mid = (left + right) >> 1;
127+
if (s[mid] >= x) {
128+
right = mid;
129+
} else {
130+
left = mid + 1;
131+
}
105132
}
106-
return q.size();
133+
return left;
107134
}
108135
}
109136

@@ -124,7 +151,7 @@ public:
124151
RecentCounter() {
125152

126153
}
127-
154+
128155
int ping(int t) {
129156
q.push(t);
130157
while (q.front() < t - 3000) q.pop();
@@ -139,6 +166,28 @@ public:
139166
*/
140167
```
141168
169+
```cpp
170+
class RecentCounter {
171+
public:
172+
vector<int> s;
173+
174+
RecentCounter() {
175+
176+
}
177+
178+
int ping(int t) {
179+
s.push_back(t);
180+
return s.size() - (lower_bound(s.begin(), s.end(), t - 3000) - s.begin());
181+
}
182+
};
183+
184+
/**
185+
* Your RecentCounter object will be instantiated and called as such:
186+
* RecentCounter* obj = new RecentCounter();
187+
* int param_1 = obj->ping(t);
188+
*/
189+
```
190+
142191
### **Go**
143192

144193
```go
@@ -165,6 +214,39 @@ func (this *RecentCounter) Ping(t int) int {
165214
*/
166215
```
167216

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

170252
```js
@@ -200,7 +282,7 @@ public class RecentCounter {
200282
public RecentCounter() {
201283

202284
}
203-
285+
204286
public int Ping(int t) {
205287
q.Enqueue(t);
206288
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 len(self.s) - 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 idx - 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 s.size() - (lower_bound(s.begin(), s.end(), t - 3000) - s.begin());
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 len(this.s) - 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)