Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d073a92

Browse files
committed
feat: add solutions to leetcode problem: No.0359. Logger Rate Limiter
1 parent 49e76b9 commit d073a92

File tree

9 files changed

+318
-25
lines changed

9 files changed

+318
-25
lines changed

‎.github/workflows/contributors.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ jobs:
1717
svgWidth: 890
1818
round: true
1919
includeBots: true
20+
commitMessage: 'chore: update contributors to @doocs/leetcode'

‎solution/0300-0399/0359.Logger Rate Limiter/README.md‎

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,111 @@ logger.shouldPrintMessage(11, "foo"); // 11 >= 11 ,返回 true ,下一次 "f
4949
<li>最多调用 <code>10<sup>4</sup></code> 次 <code>shouldPrintMessage</code> 方法</li>
5050
</ul>
5151

52-
5352
## 解法
5453

5554
<!-- 这里可写通用的实现逻辑 -->
5655

56+
哈希表实现。
57+
5758
<!-- tabs:start -->
5859

5960
### **Python3**
6061

6162
<!-- 这里可写当前语言的特殊实现逻辑 -->
6263

6364
```python
64-
65+
class Logger:
66+
67+
def __init__(self):
68+
"""
69+
Initialize your data structure here.
70+
"""
71+
self.limiter = {}
72+
73+
def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
74+
"""
75+
Returns true if the message should be printed in the given timestamp, otherwise returns false.
76+
If this method returns false, the message will not be printed.
77+
The timestamp is in seconds granularity.
78+
"""
79+
t = self.limiter.get(message, 0)
80+
if t > timestamp:
81+
return False
82+
self.limiter[message] = timestamp + 10
83+
return True
84+
85+
86+
# Your Logger object will be instantiated and called as such:
87+
# obj = Logger()
88+
# param_1 = obj.shouldPrintMessage(timestamp,message)
6589
```
6690

6791
### **Java**
6892

6993
<!-- 这里可写当前语言的特殊实现逻辑 -->
7094

7195
```java
96+
class Logger {
97+
98+
private Map<String, Integer> limiter;
99+
100+
/** Initialize your data structure here. */
101+
public Logger() {
102+
limiter = new HashMap<>();
103+
}
104+
105+
/** Returns true if the message should be printed in the given timestamp, otherwise returns false.
106+
If this method returns false, the message will not be printed.
107+
The timestamp is in seconds granularity. */
108+
public boolean shouldPrintMessage(int timestamp, String message) {
109+
int t = limiter.getOrDefault(message, 0);
110+
if (t > timestamp) {
111+
return false;
112+
}
113+
limiter.put(message, timestamp + 10);
114+
return true;
115+
}
116+
}
117+
118+
/**
119+
* Your Logger object will be instantiated and called as such:
120+
* Logger obj = new Logger();
121+
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
122+
*/
123+
```
72124

125+
### **JavaScript**
126+
127+
```js
128+
/**
129+
* Initialize your data structure here.
130+
*/
131+
var Logger = function () {
132+
this.limiter = {};
133+
};
134+
135+
/**
136+
* Returns true if the message should be printed in the given timestamp, otherwise returns false.
137+
If this method returns false, the message will not be printed.
138+
The timestamp is in seconds granularity.
139+
* @param {number} timestamp
140+
* @param {string} message
141+
* @return {boolean}
142+
*/
143+
Logger.prototype.shouldPrintMessage = function (timestamp, message) {
144+
const t = this.limiter[message] || 0;
145+
if (t > timestamp) {
146+
return false;
147+
}
148+
this.limiter[message] = timestamp + 10;
149+
return true;
150+
};
151+
152+
/**
153+
* Your Logger object will be instantiated and called as such:
154+
* var obj = new Logger()
155+
* var param_1 = obj.shouldPrintMessage(timestamp,message)
156+
*/
73157
```
74158

75159
### **...**

‎solution/0300-0399/0359.Logger Rate Limiter/README_EN.md‎

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,103 @@ logger.shouldPrintMessage(11, &quot;foo&quot;); // 11 &gt;= 11, return true, nex
4646
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>shouldPrintMessage</code>.</li>
4747
</ul>
4848

49-
5049
## Solutions
5150

5251
<!-- tabs:start -->
5352

5453
### **Python3**
5554

5655
```python
57-
56+
class Logger:
57+
58+
def __init__(self):
59+
"""
60+
Initialize your data structure here.
61+
"""
62+
self.limiter = {}
63+
64+
def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
65+
"""
66+
Returns true if the message should be printed in the given timestamp, otherwise returns false.
67+
If this method returns false, the message will not be printed.
68+
The timestamp is in seconds granularity.
69+
"""
70+
t = self.limiter.get(message, 0)
71+
if t > timestamp:
72+
return False
73+
self.limiter[message] = timestamp + 10
74+
return True
75+
76+
77+
# Your Logger object will be instantiated and called as such:
78+
# obj = Logger()
79+
# param_1 = obj.shouldPrintMessage(timestamp,message)
5880
```
5981

6082
### **Java**
6183

6284
```java
85+
class Logger {
86+
87+
private Map<String, Integer> limiter;
88+
89+
/** Initialize your data structure here. */
90+
public Logger() {
91+
limiter = new HashMap<>();
92+
}
93+
94+
/** Returns true if the message should be printed in the given timestamp, otherwise returns false.
95+
If this method returns false, the message will not be printed.
96+
The timestamp is in seconds granularity. */
97+
public boolean shouldPrintMessage(int timestamp, String message) {
98+
int t = limiter.getOrDefault(message, 0);
99+
if (t > timestamp) {
100+
return false;
101+
}
102+
limiter.put(message, timestamp + 10);
103+
return true;
104+
}
105+
}
106+
107+
/**
108+
* Your Logger object will be instantiated and called as such:
109+
* Logger obj = new Logger();
110+
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
111+
*/
112+
```
63113

114+
### **JavaScript**
115+
116+
```js
117+
/**
118+
* Initialize your data structure here.
119+
*/
120+
var Logger = function () {
121+
this.limiter = {};
122+
};
123+
124+
/**
125+
* Returns true if the message should be printed in the given timestamp, otherwise returns false.
126+
If this method returns false, the message will not be printed.
127+
The timestamp is in seconds granularity.
128+
* @param {number} timestamp
129+
* @param {string} message
130+
* @return {boolean}
131+
*/
132+
Logger.prototype.shouldPrintMessage = function (timestamp, message) {
133+
const t = this.limiter[message] || 0;
134+
if (t > timestamp) {
135+
return false;
136+
}
137+
this.limiter[message] = timestamp + 10;
138+
return true;
139+
};
140+
141+
/**
142+
* Your Logger object will be instantiated and called as such:
143+
* var obj = new Logger()
144+
* var param_1 = obj.shouldPrintMessage(timestamp,message)
145+
*/
64146
```
65147

66148
### **...**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Logger {
2+
3+
private Map<String, Integer> limiter;
4+
5+
/** Initialize your data structure here. */
6+
public Logger() {
7+
limiter = new HashMap<>();
8+
}
9+
10+
/** Returns true if the message should be printed in the given timestamp, otherwise returns false.
11+
If this method returns false, the message will not be printed.
12+
The timestamp is in seconds granularity. */
13+
public boolean shouldPrintMessage(int timestamp, String message) {
14+
int t = limiter.getOrDefault(message, 0);
15+
if (t > timestamp) {
16+
return false;
17+
}
18+
limiter.put(message, timestamp + 10);
19+
return true;
20+
}
21+
}
22+
23+
/**
24+
* Your Logger object will be instantiated and called as such:
25+
* Logger obj = new Logger();
26+
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
27+
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Initialize your data structure here.
3+
*/
4+
var Logger = function () {
5+
this.limiter = {};
6+
};
7+
8+
/**
9+
* Returns true if the message should be printed in the given timestamp, otherwise returns false.
10+
If this method returns false, the message will not be printed.
11+
The timestamp is in seconds granularity.
12+
* @param {number} timestamp
13+
* @param {string} message
14+
* @return {boolean}
15+
*/
16+
Logger.prototype.shouldPrintMessage = function (timestamp, message) {
17+
const t = this.limiter[message] || 0;
18+
if (t > timestamp) {
19+
return false;
20+
}
21+
this.limiter[message] = timestamp + 10;
22+
return true;
23+
};
24+
25+
/**
26+
* Your Logger object will be instantiated and called as such:
27+
* var obj = new Logger()
28+
* var param_1 = obj.shouldPrintMessage(timestamp,message)
29+
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Logger:
2+
3+
def __init__(self):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
self.limiter = {}
8+
9+
def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
10+
"""
11+
Returns true if the message should be printed in the given timestamp, otherwise returns false.
12+
If this method returns false, the message will not be printed.
13+
The timestamp is in seconds granularity.
14+
"""
15+
t = self.limiter.get(message, 0)
16+
if t > timestamp:
17+
return False
18+
self.limiter[message] = timestamp + 10
19+
return True
20+
21+
22+
# Your Logger object will be instantiated and called as such:
23+
# obj = Logger()
24+
# param_1 = obj.shouldPrintMessage(timestamp,message)

‎solution/0500-0599/0554.Brick Wall/README.md‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
<li>每一行砖块的数量在&nbsp;[1,10,000] 范围内,&nbsp;墙的高度在&nbsp;[1,10,000] 范围内,&nbsp;总的砖块数量不超过 20,000。</li>
4141
</ol>
4242

43-
4443
## 解法
4544

4645
<!-- 这里可写通用的实现逻辑 -->
@@ -110,6 +109,30 @@ func leastBricks(wall [][]int) int {
110109
}
111110
```
112111

112+
### **JavaScript**
113+
114+
```js
115+
/**
116+
* @param {number[][]} wall
117+
* @return {number}
118+
*/
119+
var leastBricks = function (wall) {
120+
const cnt = new Map();
121+
for (const row of wall) {
122+
let width = 0;
123+
for (let i = 0, n = row.length - 1; i < n; ++i) {
124+
width += row[i];
125+
cnt.set(width, (cnt.get(width) || 0) + 1);
126+
}
127+
}
128+
let max = 0;
129+
for (const v of cnt.values()) {
130+
max = Math.max(max, v);
131+
}
132+
return wall.length - max;
133+
};
134+
```
135+
113136
### **...**
114137

115138
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /