Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 933cb38

Browse files
authored
feat: add solutions to lc problem: No.3450 (doocs#4054)
No.3450.Maximum Students on a Single Bench
1 parent 01be368 commit 933cb38

File tree

12 files changed

+551
-3
lines changed

12 files changed

+551
-3
lines changed

solution/1700-1799/1760.Minimum Limit of Balls in a Bag/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ tags:
4343
<pre>
4444
<strong>Input:</strong> nums = [9], maxOperations = 2
4545
<strong>Output:</strong> 3
46-
<strong>Explanation:</strong>
46+
<strong>Explanation:</strong>
4747
- Divide the bag with 9 balls into two bags of sizes 6 and 3. [<strong><u>9</u></strong>] -&gt; [6,3].
4848
- Divide the bag with 6 balls into two bags of sizes 3 and 3. [<strong><u>6</u></strong>,3] -&gt; [3,3,3].
4949
The bag with the most number of balls has 3 balls, so your penalty is 3 and you should return 3.

solution/1800-1899/1887.Reduction Operations to Make the Array Elements Equal/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ tags:
5757
<strong>输出:</strong>4
5858
<strong>解释:</strong>需要 4 次操作使 nums 中的所有元素相等:
5959
1. largest = 3 下标为 4 。nextLargest = 2 。将 nums[4] 减少到 2 。nums = [1,1,2,2,<strong>2</strong>] 。
60-
2. largest = 2 下标为 2 。nextLargest = 1 。将 nums[2] 减少到 1 。nums = [1,1,<strong>1</strong>,2,2] 。
61-
3. largest = 2 下标为 3 。nextLargest = 1 。将 nums[3] 减少到 1 。nums = [1,1,1,<strong>1</strong>,2] 。
60+
2. largest = 2 下标为 2 。nextLargest = 1 。将 nums[2] 减少到 1 。nums = [1,1,<strong>1</strong>,2,2] 。
61+
3. largest = 2 下标为 3 。nextLargest = 1 。将 nums[3] 减少到 1 。nums = [1,1,1,<strong>1</strong>,2] 。
6262
4. largest = 2 下标为 4 。nextLargest = 1 。将 nums[4] 减少到 1 。nums = [1,1,1,1,<strong>1</strong>] 。
6363
</pre>
6464

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3450.Maximum%20Students%20on%20a%20Single%20Bench/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3450. 一张长椅的上最多学生 🔒](https://leetcode.cn/problems/maximum-students-on-a-single-bench)
10+
11+
[English Version](/solution/3400-3499/3450.Maximum%20Students%20on%20a%20Single%20Bench/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p data-pm-slice="1 1 []">给定一个包含学生数据的 2 维数组&nbsp;<code>students</code>,其中&nbsp;<code>students[i] = [student_id, bench_id]</code>&nbsp;表示学生&nbsp;<code>student_id</code>&nbsp;正坐在长椅&nbsp;<code>bench_id</code>&nbsp;上。</p>
18+
19+
<p>返回单个长凳上坐着的不同学生的 <strong>最大</strong> 数量。如果没有学生,返回 0。</p>
20+
21+
<p><strong>注意:</strong>一个学生在输入中可以出现在同一张长椅上多次,但每个长椅上只能计算一次。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><span class="example-io"><b>输入:</b>students = [[1,2],[2,2],[3,3],[1,3],[2,3]]</span></p>
29+
30+
<p><span class="example-io"><b>输出:</b>3</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<ul>
35+
<li>长椅 2&nbsp;上有 2&nbsp;个不同学生:<code>[1, 2]</code>。</li>
36+
<li>长椅 3&nbsp;上有 3 个不同学生:<code>[1, 2, 3]</code>。</li>
37+
<li>一张长椅上不同学生的最大数量是 3。</li>
38+
</ul>
39+
</div>
40+
41+
<p><strong class="example">示例 2:</strong></p>
42+
43+
<div class="example-block">
44+
<p><span class="example-io"><b>输入:</b>students = [[1,1],[2,1],[3,1],[4,2],[5,2]]</span></p>
45+
46+
<p><span class="example-io"><b>输出:</b>3</span></p>
47+
48+
<p><strong>示例:</strong></p>
49+
50+
<ul>
51+
<li>长椅 1 上有 3 个不同学生:<code>[1, 2, 3]</code>。</li>
52+
<li>长椅 2 上有 2 个不同学生:<code>[4, 5]</code>。</li>
53+
<li>一张长椅上不同学生的最大数量是 3。</li>
54+
</ul>
55+
</div>
56+
57+
<p><strong class="example">示例 3:</strong></p>
58+
59+
<div class="example-block">
60+
<p><span class="example-io"><b>输入:</b>students = [[1,1],[1,1]]</span></p>
61+
62+
<p><span class="example-io"><b>输出:</b>1</span></p>
63+
64+
<p><strong>解释:</strong></p>
65+
66+
<ul>
67+
<li>一张长椅上不同学生的最大数量是 1。</li>
68+
</ul>
69+
</div>
70+
71+
<p><strong class="example">示例 4:</strong></p>
72+
73+
<div class="example-block">
74+
<p><span class="example-io"><b>输入:</b>students = []</span></p>
75+
76+
<p><span class="example-io"><b>输出:</b>0</span></p>
77+
78+
<p><strong>解释:</strong></p>
79+
80+
<ul>
81+
<li>由于不存在学生,输出为 0。</li>
82+
</ul>
83+
</div>
84+
85+
<p>&nbsp;</p>
86+
87+
<p><strong>提示:</strong></p>
88+
89+
<ul>
90+
<li><code>0 &lt;= students.length &lt;= 100</code></li>
91+
<li><code>students[i] = [student_id, bench_id]</code></li>
92+
<li><code>1 &lt;= student_id &lt;= 100</code></li>
93+
<li><code>1 &lt;= bench_id &lt;= 100</code></li>
94+
</ul>
95+
96+
<!-- description:end -->
97+
98+
## 解法
99+
100+
<!-- solution:start -->
101+
102+
### 方法一:哈希表
103+
104+
我们用一个哈希表 $d$ 来存储每个长椅上的学生,键为长椅编号,值为一个集合,集合中存储着该长椅上的学生编号。
105+
106+
遍历学生数组 $\textit{students}$,将学生编号和长椅编号存入哈希表 $d$ 中。
107+
108+
最后,我们遍历哈希表 $d$ 的值,取出集合的大小的最大值即为一张长椅上坐着的不同学生的最大数量。
109+
110+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为学生数组 $\textit{students}$ 的长度。
111+
112+
<!-- tabs:start -->
113+
114+
#### Python3
115+
116+
```python
117+
class Solution:
118+
def maxStudentsOnBench(self, students: List[List[int]]) -> int:
119+
if not students:
120+
return 0
121+
d = defaultdict(set)
122+
for student_id, bench_id in students:
123+
d[bench_id].add(student_id)
124+
return max(map(len, d.values()))
125+
```
126+
127+
#### Java
128+
129+
```java
130+
class Solution {
131+
public int maxStudentsOnBench(int[][] students) {
132+
Map<Integer, Set<Integer>> d = new HashMap<>();
133+
for (var e : students) {
134+
int studentId = e[0], benchId = e[1];
135+
d.computeIfAbsent(benchId, k -> new HashSet<>()).add(studentId);
136+
}
137+
int ans = 0;
138+
for (var s : d.values()) {
139+
ans = Math.max(ans, s.size());
140+
}
141+
return ans;
142+
}
143+
}
144+
```
145+
146+
#### C++
147+
148+
```cpp
149+
class Solution {
150+
public:
151+
int maxStudentsOnBench(vector<vector<int>>& students) {
152+
unordered_map<int, unordered_set<int>> d;
153+
for (const auto& e : students) {
154+
int studentId = e[0], benchId = e[1];
155+
d[benchId].insert(studentId);
156+
}
157+
int ans = 0;
158+
for (const auto& s : d) {
159+
ans = max(ans, (int) s.second.size());
160+
}
161+
return ans;
162+
}
163+
};
164+
```
165+
166+
#### Go
167+
168+
```go
169+
func maxStudentsOnBench(students [][]int) (ans int) {
170+
d := make(map[int]map[int]struct{})
171+
for _, e := range students {
172+
studentId, benchId := e[0], e[1]
173+
if _, exists := d[benchId]; !exists {
174+
d[benchId] = make(map[int]struct{})
175+
}
176+
d[benchId][studentId] = struct{}{}
177+
}
178+
for _, s := range d {
179+
ans = max(ans, len(s))
180+
}
181+
return
182+
}
183+
```
184+
185+
#### TypeScript
186+
187+
```ts
188+
function maxStudentsOnBench(students: number[][]): number {
189+
const d: Map<number, Set<number>> = new Map();
190+
for (const [studentId, benchId] of students) {
191+
if (!d.has(benchId)) {
192+
d.set(benchId, new Set());
193+
}
194+
d.get(benchId)?.add(studentId);
195+
}
196+
let ans = 0;
197+
for (const s of d.values()) {
198+
ans = Math.max(ans, s.size);
199+
}
200+
return ans;
201+
}
202+
```
203+
204+
#### Rust
205+
206+
```rust
207+
use std::collections::{HashMap, HashSet};
208+
209+
impl Solution {
210+
pub fn max_students_on_bench(students: Vec<Vec<i32>>) -> i32 {
211+
let mut d: HashMap<i32, HashSet<i32>> = HashMap::new();
212+
for e in students {
213+
let student_id = e[0];
214+
let bench_id = e[1];
215+
d.entry(bench_id)
216+
.or_insert_with(HashSet::new)
217+
.insert(student_id);
218+
}
219+
let mut ans = 0;
220+
for s in d.values() {
221+
ans = ans.max(s.len() as i32);
222+
}
223+
ans
224+
}
225+
}
226+
```
227+
228+
<!-- tabs:end -->
229+
230+
<!-- solution:end -->
231+
232+
<!-- problem:end -->

0 commit comments

Comments
 (0)