Find Center of Star Graph LeetCode solution in C++

1
class Solution {
2
public:
3
int findCenter(vector<vector<int>> &edges) {
4
// create an adjacency list
5
unordered_map<int, set<int>> adj;
6
int maxi;
7

8
for (int i = 0; i < edges.size(); i++) {
9
int u = edges[i][0];
10
int v = edges[i][1];
11

12
maxi = max(maxi, max(u, v));
13

14
adj[u].insert(v);
15
adj[v].insert(u);
16
}
17

18
for (auto i : adj) {
19
if (i.second.size() == maxi - 1) return i.first;
20
}
21
return -1;
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0