-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_solution.js
47 lines (39 loc) · 1.29 KB
/
my_solution.js
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
// 1971. Find if Path Exists in Graph
/**
* @param {number} n
* @param {number[][]} edges
* @param {number} source
* @param {number} destination
* @return {boolean}
*/
const validPath = (n, e, s, d) => {
let map = new Map();
for (let i = 0; i < e.length; i++) {
// console.log("e[i]")
// console.log(e[i])
// console.log(map.get(e[i][0]) || []);
// console.log((map.get(e[i][1]) || []).push(e[i][0]));
map.set(e[i][0], (map.get(e[i][0]) || []).concat(e[i][1]));
map.set(e[i][1], (map.get(e[i][1]) || []).concat(e[i][0]));
// console.log(map)
}
console.log(map)
const findInLayer = (list, count) => {
console.log("> Proc")
console.log(list, count)
if (count === n || 0 === list.length) return false;
if (list.includes(d)) return true;
for (let i = 0; i < list.length; i++) {
console.log()
count++;
return findInLayer(map.get(list[i]), 0);
}
}
return (undefined === map.get(s)) ? true : findInLayer(map.get(s), 0);
};
// let x = validPath(3, [[0, 1], [1, 2], [2, 0]], 0, 2)
// let x = validPath(6, [[0, 1], [0, 2], [3, 5], [5, 4], [4, 3]], 0, 5)
// let x = validPath(1, [], 0, 0)
let x = validPath(10, [[4, 3], [1, 4], [4, 8], [1, 7], [6, 4], [4, 2], [7, 4], [4, 0], [0, 9], [5, 4]], 5, 9)
console.log("x")
console.log(x)