I am using an array to store objects (directed edges between nodes) and would like to use the an array to store the objects such that I can use the index of the array to refer to the object id.
/**
* Take a graph where
* @param {number} N: The number of Nodes of in the graph
* @param {number[][]} trust: an array of directed edges trust [[a,b]] draws 'a trusts b'
* @return {number}
*/
var findJudge = function(N, trust) {
let nodes = [
{
trusts: {},
trustedBy: {}
}
]
trust.forEach(pair => {
nodes[pair[0]].trusts = pair[1]
nodes[pair[1]].trusted = pair[0]
})
return -1
};
Sample input: N = 2 and trust = [[1,2]] errors because nodes[2] doesn't exist yet at runtime.
I would like to avoid pre-filling the array with null values if possible in the event that N is a really large number. Perhaps this is not the proper data structure for filling sparse adjacency lists. Is there a way to say var x = new Array(of type object, of length N)?
Im open to any and all solutions.
Edit to show context of problem https://leetcode.com/problems/find-the-town-judge/description/
N? Where is it used?trustsandtrustedproperties when they already had a value. You cannot assume that a person only trusts one other person at the most. So, I think you need to review your idea.