-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaily10.cpp
39 lines (35 loc) · 1.22 KB
/
daily10.cpp
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
// Solution 1
class Solution {
public:
int findCenter(vector<vector<int>>& edges) {
// start at any node, traverse to the other side and take middle node as centre
auto path = std::vector<int>{edges[0][0]};
auto curr = edges[0][1];
for (auto i = 1; i < edges.size(); ++i) {
if (edges[i][0] != curr and edges[i][1] != curr) continue;
if (edges[i][0] == curr) {
path.push_back(edges[i][0]);
curr = edges[i][1];
} else if (edges[i][1] == curr) {
path.push_back(edges[i][1]);
curr = edges[i][0];
}
}
path.push_back(curr);
// std::cout << "path through graph is: ";
// for (auto i = path.begin(); i != path.end(); ++i) {
// std::cout << *i << " ";
// }
// std::cout << "\n";
// std::cout << path[(path.size() / 2)] << "\n";
if (path.size() % 2 == 1) return path[(path.size() / 2)];
else return path[0];
}
};
// Solution 2
class Solution {
public:
int findCenter(vector<vector<int>>& edges) {
return edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1] ? edges[0][0] : edges[0][1];
}
};