Skip to content

Commit 03eae25

Browse files
committed
leetcode
1 parent e76405b commit 03eae25

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
3+
4+
5+
-* 2359. Find Closest Node to Given Two Nodes *-
6+
7+
You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge.
8+
9+
The graph is represented with a given 0-indexed array edges of size n, indicating that there is a directed edge from node i to node edges[i]. If there is no outgoing edge from i, then edges[i] == -1.
10+
11+
You are also given two integers node1 and node2.
12+
13+
Return the index of the node that can be reached from both node1 and node2, such that the maximum between the distance from node1 to that node, and from node2 to that node is minimized. If there are multiple answers, return the node with the smallest index, and if no possible answer exists, return -1.
14+
15+
Note that edges may contain cycles.
16+
17+
18+
19+
Example 1:
20+
21+
22+
Input: edges = [2,2,3,-1], node1 = 0, node2 = 1
23+
Output: 2
24+
Explanation: The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.
25+
The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2.
26+
Example 2:
27+
28+
29+
Input: edges = [1,2,-1], node1 = 0, node2 = 2
30+
Output: 2
31+
Explanation: The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.
32+
The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2.
33+
34+
35+
Constraints:
36+
37+
n == edges.length
38+
2 <= n <= 105
39+
-1 <= edges[i] < n
40+
edges[i] != i
41+
0 <= node1, node2 < n
42+
43+
44+
*/
45+
import 'dart:math';
46+
47+
class A {
48+
int closestMeetingNode(List<int> edges, int node1, int node2) {
49+
int n = edges.length;
50+
int ans = -1;
51+
int minDist = 10000;
52+
List<int> dist1 = List.filled(n, 0);
53+
List<int> dist2 = List.filled(n, 0);
54+
List<bool> visited1 = List.filled(n, false);
55+
List<bool> visited2 = List.filled(n, false);
56+
dfs(node1, edges, dist1, visited1);
57+
dfs(node2, edges, dist2, visited2);
58+
59+
for (int currentNode = 0; currentNode < n; currentNode++) {
60+
if (visited1[currentNode] &&
61+
visited2[currentNode] &&
62+
minDist > max(dist1[currentNode], dist2[currentNode])) {
63+
minDist = max(dist1[currentNode], dist2[currentNode]);
64+
ans = currentNode;
65+
}
66+
}
67+
return ans;
68+
}
69+
70+
void dfs(int node, List<int> edges, List<int> distance, List<bool> visited) {
71+
visited[node] = true;
72+
int neighbor = edges[node];
73+
if (neighbor != -1 && !visited[neighbor]) {
74+
distance[neighbor] = distance[node] + 1;
75+
dfs(neighbor, edges, distance, visited);
76+
}
77+
}
78+
}
79+
80+
class B {
81+
List<int> getDistanceArray(List<int> edges, int node) {
82+
List<int> distanceArray = List.filled(edges.length, -1);
83+
84+
int distance = 0;
85+
while (node != -1 && distanceArray[node] == -1) {
86+
distanceArray[node] = distance++;
87+
node = edges[node];
88+
}
89+
return distanceArray;
90+
}
91+
92+
int closestMeetingNode(List<int> edges, int node1, int node2) {
93+
List<int> distanceArray1 = getDistanceArray(edges, node1);
94+
List<int> distanceArray2 = getDistanceArray(edges, node2);
95+
96+
int minDistance = 10000;
97+
int result = -1;
98+
99+
for (int i = 0; i < edges.length; i++) {
100+
if (distanceArray1[i] == -1 || distanceArray2[i] == -1) continue;
101+
102+
int distance = max(distanceArray1[i], distanceArray2[i]);
103+
if (distance < minDistance) {
104+
result = i;
105+
minDistance = distance;
106+
}
107+
}
108+
return result;
109+
}
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
func closestMeetingNode(edges []int, node1 int, node2 int) int {
4+
return bellmanFord(edges, node1, node2)
5+
}
6+
7+
func bellmanFord(edges []int, node1 int, node2 int) int {
8+
bf1 := calculateBellmanFord(node1, edges)
9+
bf2 := calculateBellmanFord(node2, edges)
10+
11+
//max Integer
12+
minPath := 1 << 31
13+
result := -1
14+
for i := range bf1 {
15+
if bf1[i] == 1<<31 || bf2[i] == 1<<31 {
16+
continue
17+
}
18+
19+
v := max(bf1[i], bf2[i])
20+
if v < minPath {
21+
minPath = v
22+
result = i
23+
}
24+
}
25+
26+
return result
27+
}
28+
29+
func calculateBellmanFord(root int, edges []int) []int {
30+
distances := make([]int, len(edges))
31+
for i := range distances {
32+
distances[i] = 1 << 31
33+
}
34+
distances[root] = 0
35+
36+
node := root
37+
for edges[node] != -1 {
38+
next := edges[node]
39+
if distances[next] > distances[node]+1 {
40+
distances[next] = distances[node] + 1
41+
node = next
42+
} else {
43+
break
44+
}
45+
}
46+
47+
return distances
48+
}
49+
50+
func max(a, b int) int {
51+
if a > b {
52+
return a
53+
}
54+
55+
return b
56+
}
57+
58+
/*
59+
60+
bellman ford:
61+
62+
root as node0:
63+
[0,MaxInt,1,2]
64+
65+
root as node1:
66+
[MaxInt,0,1,2]
67+
68+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 🔥 100% FAST 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Solution - Depth First Search
4+
5+
## Approach
6+
7+
Traverse the graph from 'node1' and 'node2' to get the distance of each connected node, respectively in separate integer array.
8+
9+
Then, find and return the node that is with the minimum max distance from 'node1' and 'node2'.
10+
11+
## Complexity
12+
13+
### Time complexity: O(n)
14+
15+
where 'n' is the number of nodes in the graph.
16+
We traverse the graph from 'node1' and 'node2' once, with the worst-case of traversing the whole graph.
17+
Additionally, we check every node for their distance to 'node1' and 'node2'.
18+
19+
### Space complexity: O(n)
20+
21+
where 'n' is the number of nodes in the graph.
22+
We created two arrays of size 'n' to record the distance of each node from 'node1' and 'node2' respectively.
23+
24+
```dart
25+
class Solution {
26+
List<int> getDistanceArray(List<int> edges, int node) {
27+
List<int> distanceArray = List.filled(edges.length, -1);
28+
29+
int distance = 0;
30+
while (node != -1 && distanceArray[node] == -1) {
31+
distanceArray[node] = distance++;
32+
node = edges[node];
33+
}
34+
return distanceArray;
35+
}
36+
37+
int closestMeetingNode(List<int> edges, int node1, int node2) {
38+
List<int> distanceArray1 = getDistanceArray(edges, node1);
39+
List<int> distanceArray2 = getDistanceArray(edges, node2);
40+
41+
int minDistance = 10000;
42+
int result = -1;
43+
44+
for (int i = 0; i < edges.length; i++) {
45+
if (distanceArray1[i] == -1 || distanceArray2[i] == -1) continue;
46+
47+
int distance = max(distanceArray1[i], distanceArray2[i]);
48+
if (distance < minDistance) {
49+
result = i;
50+
minDistance = distance;
51+
}
52+
}
53+
return result;
54+
}
55+
}
56+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
193193
- [**93.** Restore IP Addresses](RestoreIPAddresses/restore_ip_addresses.dart)
194194
- [**131.** Palindrome Partitioning](PalindromePartitioning/palindrome_partitioning.dart)
195195
- [**909.** Snakes and Ladders](SnakesAndLadders/snakes_and_ladders.dart)
196+
- [**2359.** Find Closest Node to Given Two Nodes](FindClosestNodeToGivenTwoNodes/find_closest_node_to_given_two_nodes.dart)
196197

197198
## Reach me via
198199

0 commit comments

Comments
 (0)