Last active
April 25, 2020 17:43
-
-
Save adslaton/88455fbb1ee0587ba741769e28f763f1 to your computer and use it in GitHub Desktop.
deduce-dynamic-connectivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const connections = [ // O(1) | |
{ | |
id: 1, | |
name: 1 | |
}, | |
{ | |
id: 2, | |
name: 2 | |
}, | |
{ | |
id: 3, | |
name: 3 | |
} | |
]; | |
const union = (p, q) => { // O(n) | |
connections.map((element, i) => { | |
if (element.id === p) { | |
element.id = q; | |
} | |
}) | |
}; | |
const connected = (p, q) => { // O(n) | |
let connection = false; | |
connections.map((element, i) => { | |
if (element.name === p && element.id === q) { | |
connection = true; | |
} | |
}) | |
return connection; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment