forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2359-find-closest-node-to-given-two-nodes.kt
119 lines (105 loc) · 3.59 KB
/
2359-find-closest-node-to-given-two-nodes.kt
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// bfs
class Solution {
fun closestMeetingNode(edges: IntArray, node1: Int, node2: Int): Int {
val adj = HashMap<Int, MutableList<Int>>().apply {
for ((u, v) in edges.withIndex()) {
this[u] = getOrDefault(u, mutableListOf<Int>()).apply { add(v) }
}
}
fun bfs(node: Int, distMap: HashMap<Int, Int>) {
with (LinkedList<Pair<Int, Int>>()) {
addLast(node to 0)
distMap[node] = 0
while (isNotEmpty()) {
val (node, dist) = removeFirst()
adj[node]?.forEach { nei ->
if (nei !in distMap) {
addLast(nei to dist + 1)
distMap[nei] = dist + 1
}
}
}
}
}
val node1Dist = HashMap<Int, Int>()
val node2Dist = HashMap<Int, Int>()
bfs(node1, node1Dist)
bfs(node2, node2Dist)
var res = -1
var resDist = Integer.MAX_VALUE
for (i in edges.indices) {
if (i in node1Dist && i in node2Dist) {
val dist = maxOf(node1Dist[i]!!, node2Dist[i]!!)
if (dist < resDist) {
res = i
resDist = dist
}
}
}
return res
}
}
// bfs, but omitting the adjecency graph since it's not needed. We know that every node has at most 1 outgoing edge,
// so we can use the edge list to find the next node.
class Solution {
fun closestMeetingNode(edges: IntArray, node1: Int, node2: Int): Int {
fun bfs(node: Int, distMap: HashMap<Int, Int>) {
with (LinkedList<Pair<Int, Int>>()) {
addLast(node to 0)
distMap[node] = 0
while (isNotEmpty()) {
val (node, dist) = removeFirst()
val nei = edges[node]
if (nei != -1 && nei !in distMap) {
addLast(nei to dist + 1)
distMap[nei] = dist + 1
}
}
}
}
val node1Dist = HashMap<Int, Int>()
val node2Dist = HashMap<Int, Int>()
bfs(node1, node1Dist)
bfs(node2, node2Dist)
var res = -1
var resDist = Integer.MAX_VALUE
for (i in edges.indices) {
if (i in node1Dist && i in node2Dist) {
val dist = maxOf(node1Dist[i]!!, node2Dist[i]!!)
if (dist < resDist) {
res = i
resDist = dist
}
}
}
return res
}
}
// dfs
class Solution {
fun closestMeetingNode(edges: IntArray, node1: Int, node2: Int): Int {
fun dfs(node: Int, distMap: HashMap<Int, Int>, dist: Int) {
val nei = edges[node]
if (nei != -1 && nei !in distMap) {
distMap[nei] = dist + 1
dfs(nei, distMap, dist + 1)
}
}
val node1Dist = HashMap<Int, Int>().apply { this[node1] = 0 }
val node2Dist = HashMap<Int, Int>().apply { this[node2] = 0 }
dfs(node1, node1Dist, 0)
dfs(node2, node2Dist, 0)
var res = -1
var resDist = Integer.MAX_VALUE
for (i in edges.indices) {
if (i in node1Dist && i in node2Dist) {
val dist = maxOf(node1Dist[i]!!, node2Dist[i]!!)
if (dist < resDist) {
res = i
resDist = dist
}
}
}
return res
}
}