Skip to content

Commit 246e3b4

Browse files
authored
feat: add weekly contest 400 (#2999)
1 parent d7bc796 commit 246e3b4

File tree

42 files changed

+2787
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2787
-1
lines changed

solution/1500-1599/1521.Find a Value of a Mysterious Function Closest to Target/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ tags:
7575

7676
时间复杂度 $O(n \times \log M)$,空间复杂度 $O(\log M)$。其中 $n$ 和 $M$ 分别是数组 $arr$ 的长度和数组 $arr$ 中的最大值。
7777

78+
相似题目:
79+
80+
- [3171. 找到按位与最接近 K 的子数组](/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20AND%20Closest%20to%20K/README.md)
81+
7882
<!-- tabs:start -->
7983

8084
#### Python3

solution/1500-1599/1521.Find a Value of a Mysterious Function Closest to Target/README_EN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ If we fix the right endpoint $r$ each time, then the range of the left endpoint
7676

7777
The time complexity is $O(n \times \log M)$, and the space complexity is $O(\log M)$. Here, $n$ and $M$ are the length of the array $arr$ and the maximum value in the array $arr$, respectively.
7878

79+
Similar problems:
80+
81+
- [3171. Find Subarray With Bitwise AND Closest to K](/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20AND%20Closest%20to%20K/README_EN.md)
82+
7983
<!-- tabs:start -->
8084

8185
#### Python3

solution/3000-3099/3097.Shortest Subarray With OR at Least K II/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ tags:
9292

9393
时间复杂度 $O(n \times \log M)$,空间复杂度 $O(\log M)$,其中 $n$ 和 $M$ 分别是数组的长度和数组中元素的最大值。
9494

95+
相似题目:
96+
97+
- [3171. 找到按位与最接近 K 的子数组](/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20AND%20Closest%20to%20K/README.md)
98+
9599
<!-- tabs:start -->
96100

97101
#### Python3

solution/3000-3099/3097.Shortest Subarray With OR at Least K II/README_EN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ Finally, we return the minimum length. If there is no subarray that meets the co
9090

9191
The time complexity is $O(n \times \log M)$ and the space complexity is $O(\log M)$, where $n$ and $M$ are the length of the array and the maximum value of the elements in the array, respectively.
9292

93+
Similar Problems:
94+
95+
- [3171. Find Subarray With Bitwise AND Closest to K](/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20AND%20Closest%20to%20K/README_EN.md)
96+
9397
<!-- tabs:start -->
9498

9599
#### Python3
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3168.Minimum%20Number%20of%20Chairs%20in%20a%20Waiting%20Room/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3168. 候诊室中的最少椅子数](https://leetcode.cn/problems/minimum-number-of-chairs-in-a-waiting-room)
10+
11+
[English Version](/solution/3100-3199/3168.Minimum%20Number%20of%20Chairs%20in%20a%20Waiting%20Room/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个字符串 <code>s</code>,模拟每秒钟的事件 <code>i</code>:</p>
18+
19+
<ul>
20+
<li>如果 <code>s[i] == 'E'</code>,表示有一位顾客进入候诊室并占用一把椅子。</li>
21+
<li>如果 <code>s[i] == 'L'</code>,表示有一位顾客离开候诊室,从而释放一把椅子。</li>
22+
</ul>
23+
24+
<p>返回保证每位进入候诊室的顾客都能有椅子坐的<strong> 最少 </strong>椅子数,假设候诊室最初是 <strong>空的 </strong>。</p>
25+
26+
<p>&nbsp;</p>
27+
28+
<p><strong class="example">示例 1:</strong></p>
29+
30+
<div class="example-block">
31+
<p><strong>输入:</strong><span class="example-io">s = "EEEEEEE"</span></p>
32+
33+
<p><strong>输出:</strong><span class="example-io">7</span></p>
34+
35+
<p><strong>解释:</strong></p>
36+
37+
<p>每秒后都有一个顾客进入候诊室,没有人离开。因此,至少需要 7 把椅子。</p>
38+
</div>
39+
40+
<p><strong class="example">示例 2:</strong></p>
41+
42+
<div class="example-block">
43+
<p><strong>输入:</strong><span class="example-io">s = "ELELEEL"</span></p>
44+
45+
<p><strong>输出:</strong><span class="example-io">2</span></p>
46+
47+
<p><strong>解释:</strong></p>
48+
49+
<p>假设候诊室里有 2 把椅子。下表显示了每秒钟等候室的状态。</p>
50+
</div>
51+
<table>
52+
<tbody>
53+
<tr>
54+
<th>秒</th>
55+
<th>事件</th>
56+
<th>候诊室的人数</th>
57+
<th>可用的椅子数</th>
58+
</tr>
59+
<tr>
60+
<td>0</td>
61+
<td>Enter</td>
62+
<td>1</td>
63+
<td>1</td>
64+
</tr>
65+
<tr>
66+
<td>1</td>
67+
<td>Leave</td>
68+
<td>0</td>
69+
<td>2</td>
70+
</tr>
71+
<tr>
72+
<td>2</td>
73+
<td>Enter</td>
74+
<td>1</td>
75+
<td>1</td>
76+
</tr>
77+
<tr>
78+
<td>3</td>
79+
<td>Leave</td>
80+
<td>0</td>
81+
<td>2</td>
82+
</tr>
83+
<tr>
84+
<td>4</td>
85+
<td>Enter</td>
86+
<td>1</td>
87+
<td>1</td>
88+
</tr>
89+
<tr>
90+
<td>5</td>
91+
<td>Enter</td>
92+
<td>2</td>
93+
<td>0</td>
94+
</tr>
95+
<tr>
96+
<td>6</td>
97+
<td>Leave</td>
98+
<td>1</td>
99+
<td>1</td>
100+
</tr>
101+
</tbody>
102+
</table>
103+
104+
<p><strong class="example">示例 3:</strong></p>
105+
106+
<div class="example-block">
107+
<p><strong>输入:</strong><span class="example-io">s = "ELEELEELLL"</span></p>
108+
109+
<p><strong>输出:</strong><span class="example-io">3</span></p>
110+
111+
<p><strong>解释:</strong></p>
112+
113+
<p>假设候诊室里有 3 把椅子。下表显示了每秒钟等候室的状态。</p>
114+
</div>
115+
<table>
116+
<tbody>
117+
<tr>
118+
<th>秒</th>
119+
<th>事件</th>
120+
<th>候诊室的人数</th>
121+
<th>可用的椅子数</th>
122+
</tr>
123+
<tr>
124+
<td>0</td>
125+
<td>Enter</td>
126+
<td>1</td>
127+
<td>2</td>
128+
</tr>
129+
<tr>
130+
<td>1</td>
131+
<td>Leave</td>
132+
<td>0</td>
133+
<td>3</td>
134+
</tr>
135+
<tr>
136+
<td>2</td>
137+
<td>Enter</td>
138+
<td>1</td>
139+
<td>2</td>
140+
</tr>
141+
<tr>
142+
<td>3</td>
143+
<td>Enter</td>
144+
<td>2</td>
145+
<td>1</td>
146+
</tr>
147+
<tr>
148+
<td>4</td>
149+
<td>Leave</td>
150+
<td>1</td>
151+
<td>2</td>
152+
</tr>
153+
<tr>
154+
<td>5</td>
155+
<td>Enter</td>
156+
<td>2</td>
157+
<td>1</td>
158+
</tr>
159+
<tr>
160+
<td>6</td>
161+
<td>Enter</td>
162+
<td>3</td>
163+
<td>0</td>
164+
</tr>
165+
<tr>
166+
<td>7</td>
167+
<td>Leave</td>
168+
<td>2</td>
169+
<td>1</td>
170+
</tr>
171+
<tr>
172+
<td>8</td>
173+
<td>Leave</td>
174+
<td>1</td>
175+
<td>2</td>
176+
</tr>
177+
<tr>
178+
<td>9</td>
179+
<td>Leave</td>
180+
<td>0</td>
181+
<td>3</td>
182+
</tr>
183+
</tbody>
184+
</table>
185+
186+
<p>&nbsp;</p>
187+
188+
<p><strong>提示:</strong></p>
189+
190+
<ul>
191+
<li><code>1 &lt;= s.length &lt;= 50</code></li>
192+
<li><code>s</code> 仅由字母 <code>'E'</code> 和 <code>'L'</code> 组成。</li>
193+
<li><code>s</code> 表示一个有效的进出序列。</li>
194+
</ul>
195+
196+
<!-- description:end -->
197+
198+
## 解法
199+
200+
<!-- solution:start -->
201+
202+
### 方法一:模拟
203+
204+
我们用变量 $\text{cnt}$ 来记录当前需要的椅子数,用变量 $\text{left}$ 来记录当前剩余的空椅子数。遍历字符串 $\text{s}$,如果当前字符是 'E',那么如果有剩余的空椅子,就直接使用一个空椅子,否则需要增加一个椅子;如果当前字符是 'L',那么剩余的空椅子数加一。
205+
206+
遍历结束后,返回 $\text{cnt}$ 即可。
207+
208+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $\text{s}$ 的长度。空间复杂度 $O(1)$。
209+
210+
<!-- tabs:start -->
211+
212+
#### Python3
213+
214+
```python
215+
class Solution:
216+
def minimumChairs(self, s: str) -> int:
217+
cnt = left = 0
218+
for c in s:
219+
if c == "E":
220+
if left:
221+
left -= 1
222+
else:
223+
cnt += 1
224+
else:
225+
left += 1
226+
return cnt
227+
```
228+
229+
#### Java
230+
231+
```java
232+
class Solution {
233+
public int minimumChairs(String s) {
234+
int cnt = 0, left = 0;
235+
for (int i = 0; i < s.length(); ++i) {
236+
if (s.charAt(i) == 'E') {
237+
if (left > 0) {
238+
--left;
239+
} else {
240+
++cnt;
241+
}
242+
} else {
243+
++left;
244+
}
245+
}
246+
return cnt;
247+
}
248+
}
249+
```
250+
251+
#### C++
252+
253+
```cpp
254+
class Solution {
255+
public:
256+
int minimumChairs(string s) {
257+
int cnt = 0, left = 0;
258+
for (char& c : s) {
259+
if (c == 'E') {
260+
if (left > 0) {
261+
--left;
262+
} else {
263+
++cnt;
264+
}
265+
} else {
266+
++left;
267+
}
268+
}
269+
return cnt;
270+
}
271+
};
272+
```
273+
274+
#### Go
275+
276+
```go
277+
func minimumChairs(s string) int {
278+
cnt, left := 0, 0
279+
for _, c := range s {
280+
if c == 'E' {
281+
if left > 0 {
282+
left--
283+
} else {
284+
cnt++
285+
}
286+
} else {
287+
left++
288+
}
289+
}
290+
return cnt
291+
}
292+
```
293+
294+
#### TypeScript
295+
296+
```ts
297+
function minimumChairs(s: string): number {
298+
let [cnt, left] = [0, 0];
299+
for (const c of s) {
300+
if (c === 'E') {
301+
if (left > 0) {
302+
--left;
303+
} else {
304+
++cnt;
305+
}
306+
} else {
307+
++left;
308+
}
309+
}
310+
return cnt;
311+
}
312+
```
313+
314+
<!-- tabs:end -->
315+
316+
<!-- solution:end -->
317+
318+
<!-- problem:end -->

0 commit comments

Comments
 (0)