[JavaScript] 2192. All Ancestors of a Node in a Directed Acyclic Graph
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\nMy old solution\n\n...\nvariable names\n...\np = parent\nps = parent se
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\nMy old solution\n\n...\nvariable names\n...\n**p = parent\nps = parent set\naps = array of parent set\ngp = grand parent**\n\n...\nalgorithm\n...\n**1 create parents\n2 add grand parents to parents\n3 sort & return\n...**\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n---\n\n```\nvar getAncestors = function (n, ed) {\n  let aps = Array(n).fill().map(() => new Set())\n  for (let [u, v] of ed) {\n    aps[v].add(u)\n  }\n  for (let ps of aps) {\n    for (let p of ps) {\n      for (let gp of aps[p]) {\n        ps.add(gp)\n      }\n    }\n  }\n  return aps.map(ps => [...ps].sort((a, b) => a - b))\n}\n```\n\n---\n\n```\nvar getAncestors = function (n, edges) {\n  let g = Array(n).fill().map(() => new Set())\n  for (let [u, v] of edges) {\n    g[v].add(u)\n  }\n  for (let parents of g) {\n    for (let parent of parents) {\n      for (let grand_parent of g[parent]) {\n        parents.add(grand_parent)\n      }\n    }\n  }\n  return g.map(parents => [...parents].sort((a, b) => a - b))\n}\n```\n\n---\n
['JavaScript']