Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created May 17, 2017 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/8b71ec5a249f454d8130bd7b15c546fd to your computer and use it in GitHub Desktop.
Save jianminchen/8b71ec5a249f454d8130bd7b15c546fd to your computer and use it in GitHub Desktop.
Leetcode 582 - kill process - pass all test cases
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Leetcode582_KillProcess
{
/// <summary>
/// problem statement:
/// https://leetcode.com/problems/kill-process/#/description
/// </summary>
class Program
{
internal class Node
{
public int Id { get; set; }
public int ParentId { get; set; }
public HashSet<int> children { get; set; }
public Node(int id)
{
Id = id;
}
public void AddChild(int id)
{
if (children == null)
{
children = new HashSet<int>();
}
children.Add(id);
}
}
static void Main(string[] args)
{
RunTestcase();
}
public static void RunTestcase()
{
IList<int> pid = new List<int>() { 1, 3, 10, 5 };
IList<int> ppid = new List<int>() { 3, 0, 5, 3};
var result = KillProcess(pid, ppid, 5);
}
public static void RunTestcase2()
{
IList<int> pid = new List<int>() { 1, 2, 3 };
IList<int> ppid = new List<int>() { 0, 1, 2 };
var result = KillProcess(pid, ppid, 1);
}
public static IList<int> KillProcess(IList<int> pid, IList<int> ppid, int kill)
{
IList<int> children = new List<int>();
// create nodes in the tree one by one
IList<Node> nodes = new List<Node>();
foreach (var item in pid)
{
var current = new Node(item);
nodes.Add(current);
}
var lookup = getHashSet(pid);
// build the tree's children and parent node info, get root node info
for (int i = 0; i < ppid.Count; i++)
{
var parentId = ppid[i];
var id = nodes[i].Id;
nodes[i].ParentId = ppid[i];
if (parentId == 0)
{
continue;
}
// add child node to node with id
int index = lookup[parentId];
nodes[index].AddChild(id);
}
// find kill Id's children
return getKillChildren(lookup, nodes, kill);
}
/// <summary>
/// Dictionary is important to avoid timeout, Array.IndexOf does not work.
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
private static Dictionary<int, int> getHashSet(IList<int> ids)
{
var lookup = new Dictionary<int, int>();
for (int i = 0; i < ids.Count; i++)
{
lookup.Add(ids[i], i);
}
return lookup;
}
/// <summary>
/// BFS search
/// </summary>
/// <param name="lookup"></param>
/// <param name="nodes"></param>
/// <param name="kill"></param>
/// <returns></returns>
private static IList<int> getKillChildren(Dictionary<int,int> lookup, IList<Node> nodes, int kill)
{
IList<int> children = new List<int>();
int index = lookup[kill];
int id = nodes[index].Id;
var queue = new Queue<int>();
queue.Enqueue(id);
while (queue.Count > 0 )
{
int visitId = queue.Dequeue();
children.Add(visitId);
index = lookup[visitId];
var visitNode = nodes[index];
var childrenNodes = visitNode.children;
if (childrenNodes != null) // need more attention here!
{
foreach (var item in childrenNodes)
{
queue.Enqueue(item);
}
}
}
var array = children.ToArray();
Array.Sort(array);
return array.ToList();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment