Skip to content

feat: add weekly contest 400 #2999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ tags:

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

相似题目:

- [3171. 找到按位与最接近 K 的子数组](/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20AND%20Closest%20to%20K/README.md)

<!-- tabs:start -->

#### Python3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ If we fix the right endpoint $r$ each time, then the range of the left endpoint

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.

Similar problems:

- [3171. Find Subarray With Bitwise AND Closest to K](/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20AND%20Closest%20to%20K/README_EN.md)

<!-- tabs:start -->

#### Python3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ tags:

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

相似题目:

- [3171. 找到按位与最接近 K 的子数组](/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20AND%20Closest%20to%20K/README.md)

<!-- tabs:start -->

#### Python3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Finally, we return the minimum length. If there is no subarray that meets the co

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.

Similar Problems:

- [3171. Find Subarray With Bitwise AND Closest to K](/solution/3100-3199/3171.Find%20Subarray%20With%20Bitwise%20AND%20Closest%20to%20K/README_EN.md)

<!-- tabs:start -->

#### Python3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3168.Minimum%20Number%20of%20Chairs%20in%20a%20Waiting%20Room/README.md
---

<!-- problem:start -->

# [3168. 候诊室中的最少椅子数](https://leetcode.cn/problems/minimum-number-of-chairs-in-a-waiting-room)

[English Version](/solution/3100-3199/3168.Minimum%20Number%20of%20Chairs%20in%20a%20Waiting%20Room/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你一个字符串 <code>s</code>,模拟每秒钟的事件 <code>i</code>:</p>

<ul>
<li>如果 <code>s[i] == 'E'</code>,表示有一位顾客进入候诊室并占用一把椅子。</li>
<li>如果 <code>s[i] == 'L'</code>,表示有一位顾客离开候诊室,从而释放一把椅子。</li>
</ul>

<p>返回保证每位进入候诊室的顾客都能有椅子坐的<strong> 最少 </strong>椅子数,假设候诊室最初是 <strong>空的 </strong>。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">s = "EEEEEEE"</span></p>

<p><strong>输出:</strong><span class="example-io">7</span></p>

<p><strong>解释:</strong></p>

<p>每秒后都有一个顾客进入候诊室,没有人离开。因此,至少需要 7 把椅子。</p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">s = "ELELEEL"</span></p>

<p><strong>输出:</strong><span class="example-io">2</span></p>

<p><strong>解释:</strong></p>

<p>假设候诊室里有 2 把椅子。下表显示了每秒钟等候室的状态。</p>
</div>
<table>
<tbody>
<tr>
<th>秒</th>
<th>事件</th>
<th>候诊室的人数</th>
<th>可用的椅子数</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>Leave</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>Enter</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>6</td>
<td>Leave</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>

<p><strong class="example">示例 3:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">s = "ELEELEELLL"</span></p>

<p><strong>输出:</strong><span class="example-io">3</span></p>

<p><strong>解释:</strong></p>

<p>假设候诊室里有 3 把椅子。下表显示了每秒钟等候室的状态。</p>
</div>
<table>
<tbody>
<tr>
<th>秒</th>
<th>事件</th>
<th>候诊室的人数</th>
<th>可用的椅子数</th>
</tr>
<tr>
<td>0</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>Enter</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>5</td>
<td>Enter</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>6</td>
<td>Enter</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td>Leave</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td>Leave</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>9</td>
<td>Leave</td>
<td>0</td>
<td>3</td>
</tr>
</tbody>
</table>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= s.length &lt;= 50</code></li>
<li><code>s</code> 仅由字母 <code>'E'</code> 和 <code>'L'</code> 组成。</li>
<li><code>s</code> 表示一个有效的进出序列。</li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:模拟

我们用变量 $\text{cnt}$ 来记录当前需要的椅子数,用变量 $\text{left}$ 来记录当前剩余的空椅子数。遍历字符串 $\text{s}$,如果当前字符是 'E',那么如果有剩余的空椅子,就直接使用一个空椅子,否则需要增加一个椅子;如果当前字符是 'L',那么剩余的空椅子数加一。

遍历结束后,返回 $\text{cnt}$ 即可。

时间复杂度 $O(n)$,其中 $n$ 为字符串 $\text{s}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def minimumChairs(self, s: str) -> int:
cnt = left = 0
for c in s:
if c == "E":
if left:
left -= 1
else:
cnt += 1
else:
left += 1
return cnt
```

#### Java

```java
class Solution {
public int minimumChairs(String s) {
int cnt = 0, left = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == 'E') {
if (left > 0) {
--left;
} else {
++cnt;
}
} else {
++left;
}
}
return cnt;
}
}
```

#### C++

```cpp
class Solution {
public:
int minimumChairs(string s) {
int cnt = 0, left = 0;
for (char& c : s) {
if (c == 'E') {
if (left > 0) {
--left;
} else {
++cnt;
}
} else {
++left;
}
}
return cnt;
}
};
```

#### Go

```go
func minimumChairs(s string) int {
cnt, left := 0, 0
for _, c := range s {
if c == 'E' {
if left > 0 {
left--
} else {
cnt++
}
} else {
left++
}
}
return cnt
}
```

#### TypeScript

```ts
function minimumChairs(s: string): number {
let [cnt, left] = [0, 0];
for (const c of s) {
if (c === 'E') {
if (left > 0) {
--left;
} else {
++cnt;
}
} else {
++left;
}
}
return cnt;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading