-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3450-MaximumStudentsOnASingleBench.go
More file actions
94 lines (83 loc) · 2.92 KB
/
3450-MaximumStudentsOnASingleBench.go
File metadata and controls
94 lines (83 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
// 3450. Maximum Students on a Single Bench
// You are given a 2D integer array of student data students,
// where students[i] = [student_id, bench_id] represents that student student_id is sitting on the bench bench_id.
// Return the maximum number of unique students sitting on any single bench.
// If no students are present, return 0.
// Note: A student can appear multiple times on the same bench in the input, but they should be counted only once per bench.
// Example 1:
// Input: students = [[1,2],[2,2],[3,3],[1,3],[2,3]]
// Output: 3
// Explanation:
// Bench 2 has two unique students: [1, 2].
// Bench 3 has three unique students: [1, 2, 3].
// The maximum number of unique students on a single bench is 3.
// Example 2:
// Input: students = [[1,1],[2,1],[3,1],[4,2],[5,2]]
// Output: 3
// Explanation:
// Bench 1 has three unique students: [1, 2, 3].
// Bench 2 has two unique students: [4, 5].
// The maximum number of unique students on a single bench is 3.
// Example 3:
// Input: students = [[1,1],[1,1]]
// Output: 1
// Explanation:
// The maximum number of unique students on a single bench is 1.
// Example 4:
// Input: students = []
// Output: 0
// Explanation:
// Since no students are present, the output is 0.
// Constraints:
// 0 <= students.length <= 100
// students[i] = [student_id, bench_id]
// 1 <= student_id <= 100
// 1 <= bench_id <= 100
import "fmt"
func maxStudentsOnBench(students [][]int) int {
mp := make(map[int]map[int]int)
for _, v:= range students {
a, b := v[0], v[1]
if _, ok := mp[b]; !ok {
mp[b] = make(map[int]int)
}
mp[b][a]++
}
res := 0
max := func (x, y int) int { if x > y { return x; }; return y; }
for _,v := range mp {
res = max(res, len(v))
}
return res
}
func main() {
// Example 1:
// Input: students = [[1,2],[2,2],[3,3],[1,3],[2,3]]
// Output: 3
// Explanation:
// Bench 2 has two unique students: [1, 2].
// Bench 3 has three unique students: [1, 2, 3].
// The maximum number of unique students on a single bench is 3.
fmt.Println(maxStudentsOnBench([][]int{{1,2},{2,2},{3,3},{1,3},{2,3}})) // 3
// Example 2:
// Input: students = [[1,1],[2,1],[3,1],[4,2],[5,2]]
// Output: 3
// Explanation:
// Bench 1 has three unique students: [1, 2, 3].
// Bench 2 has two unique students: [4, 5].
// The maximum number of unique students on a single bench is 3.
fmt.Println(maxStudentsOnBench([][]int{{1,1},{2,1},{3,1},{4,2},{5,2}})) // 3
// Example 3:
// Input: students = [[1,1],[1,1]]
// Output: 1
// Explanation:
// The maximum number of unique students on a single bench is 1.
fmt.Println(maxStudentsOnBench([][]int{{1,1},{1,1}})) // 1
// Example 4:
// Input: students = []
// Output: 0
// Explanation:
// Since no students are present, the output is 0.
fmt.Println(maxStudentsOnBench([][]int{})) // 0
}