Skip to content

Commit 22cbe0d

Browse files
add 582
1 parent 087c1e4 commit 22cbe0d

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|582|[Kill Process](https://leetcode.com/problems/kill-process/)|[Solution](../master/src/main/java/com/stevesun/solutions/_582.java) | O(n) |O(h) | Medium | Stack
2324
|581|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/)|[Solution](../master/src/main/java/com/stevesun/solutions/_581.java) | O(n) |O(1) | Easy | Array, Sort
2425
|575|[Distribute Candies](https://leetcode.com/problems/distribute-candies/)|[Solution](../master/src/main/java/com/stevesun/solutions/_575.java) | O(nlogn) |O(1) | Easy | Array
2526
|572|[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)|[Solution](../master/src/main/java/com/stevesun/solutions/_572.java) | O(m*n) |O(1) | Easy | Tree
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 582. Kill Process
7+
*
8+
* Given n processes, each process has a unique PID (process id) and its PPID (parent process id).
9+
10+
Each process only has one parent process, but may have one or more children processes. This is just like a tree structure. Only one process has PPID that is 0, which means this process has no parent process. All the PIDs will be distinct positive integers.
11+
12+
We use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID.
13+
14+
Now given the two lists, and a PID representing a process you want to kill, return a list of PIDs of processes that will be killed in the end. You should assume that when a process is killed, all its children processes will be killed. No order is required for the final answer.
15+
16+
Example 1:
17+
Input:
18+
pid = [1, 3, 10, 5]
19+
ppid = [3, 0, 5, 3]
20+
kill = 5
21+
Output: [5,10]
22+
23+
Explanation:
24+
3
25+
/ \
26+
1 5
27+
/
28+
10
29+
30+
Kill 5 will also kill 10.
31+
32+
Note:
33+
The given kill id is guaranteed to be one of the given PIDs.
34+
n >= 1.
35+
*/
36+
public class _582 {
37+
38+
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
39+
Map<Integer, List<Integer>> map = new HashMap<>();
40+
for (int i = 0; i < pid.size(); i++) {
41+
map.putIfAbsent(ppid.get(i), new LinkedList<>());
42+
map.get(ppid.get(i)).add(pid.get(i));
43+
}
44+
List<Integer> result = new LinkedList<>();
45+
Deque<Integer> stack = new ArrayDeque<>();
46+
stack.offer(kill);
47+
while (!stack.isEmpty()) {
48+
int curr = stack.poll();
49+
result.add(curr);
50+
List<Integer> list = map.get(curr);
51+
if (list != null) stack.addAll(list);
52+
}
53+
return result;
54+
}
55+
56+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions._48;
4+
import com.stevesun.solutions._582;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
import static org.junit.Assert.assertEquals;
13+
14+
/**
15+
* Created by stevesun on 5/18/17.
16+
*/
17+
public class _582Test {
18+
private static _582 test;
19+
private static List<Integer> pid;
20+
private static List<Integer> ppid;
21+
private static List<Integer> expected;
22+
private static Integer kill;
23+
24+
@BeforeClass
25+
public static void setup(){
26+
test = new _582();
27+
}
28+
29+
@Test
30+
public void test1(){
31+
pid = Arrays.asList(1,3,10,5);
32+
ppid = Arrays.asList(3,0,5,3);
33+
kill = 5;
34+
expected = Arrays.asList(5,10);
35+
assertEquals(expected, test.killProcess(pid, ppid, kill));
36+
}
37+
38+
@Test
39+
public void test2(){
40+
pid = Arrays.asList(1,3,10,5);
41+
ppid = Arrays.asList(3,0,5,3);
42+
kill = 3;
43+
expected = Arrays.asList(3,1,5,10);
44+
assertEquals(expected, test.killProcess(pid, ppid, kill));
45+
}
46+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy