-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloest_meeting_node.cpp
More file actions
86 lines (77 loc) · 2.09 KB
/
cloest_meeting_node.cpp
File metadata and controls
86 lines (77 loc) · 2.09 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
/*
* =====================================================================================
*
* Filename: cloest_meeting_node.cpp
*
* Description: 2359. Find Closest Node to Given Two Nodes
* https://leetcode.com/problems/find-closest-node-to-given-two-nodes/
*
* Version: 1.0
* Created: 05/31/2025 23:58:45
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <queue>
#include <tuple>
#include <vector>
#include "gtest/gtest.h"
class Solution {
public:
int closestMeetingNode(std::vector<int>& edges, int node1, int node2) {
if (node1 == node2) {
return node1;
}
const int size = edges.size();
std::vector<int> path1(size, -1);
std::vector<int> path2(size, -1);
// Breadth first search from node1, node2
bfsVisit(edges, node1, path1);
bfsVisit(edges, node2, path2);
int min_dis = INT_MAX;
int index = -1;
for (int i = 0; i < size; i++) {
if (path1[i] == -1 || path2[i] == -1) {
continue;
}
const int cur_dis = std::max(path1[i], path2[i]);
if (cur_dis < min_dis) {
min_dis = cur_dis;
index = i;
}
}
return index;
}
private:
void bfsVisit(const std::vector<int>& edges, const int from, std::vector<int>& path) {
std::queue<int> nodes;
int distance = 0;
nodes.push(from);
while (!nodes.empty()) {
const int i = nodes.front();
if (path[i] != -1) {
// Visited
break;
}
nodes.pop();
path[i] = distance++;
if (edges[i] != -1) {
nodes.push(edges[i]);
}
}
}
};
TEST(Solution, closestMeetingNode) {
std::vector<std::tuple<std::vector<int>, int, int, int>> cases = {
{{2, 2, 3, -1}, 0, 1, 2},
{{1, 2, -1}, 0, 2, 2},
{{4, 4, 8, -1, 9, 8, 4, 4, 1, 1}, 5, 6, 1},
};
for (auto& [edges, node1, node2, index] : cases) {
EXPECT_EQ(Solution().closestMeetingNode(edges, node1, node2), index);
}
}