当前位置:网站首页>[crampon programming] lintcode decoding Encyclopedia - 872 termination process
[crampon programming] lintcode decoding Encyclopedia - 872 termination process
2022-07-05 04:31:00 【BZIClaw】
【 Crampon programming 】LintCode Decoding Encyclopedia —— 872 Terminate the process
author :BZIClaw
872 Terminate the process
Python:
class Solution:
""" @param pid: the process id @param ppid: the parent process id @param kill: a PID you want to kill @return: a list of PIDs of processes that will be killed in the end """
def killProcess(self, pid, ppid, kill):
m={
}
for i,parent in enumerate(ppid):
if parent not in m:
m[parent] = []
m[parent].append(pid[i])
ans=[]
que=[kill]
while que:
cur = que.pop(0)
ans.append(cur)
if cur in m:
que +=m[cur]
return ans
# Write your code here
Java:
public class Solution {
/** * @param pid: the process id * @param ppid: the parent process id * @param kill: a PID you want to kill * @return: a list of PIDs of processes that will be killed in the end */
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
Map<Integer, List<Integer>> map = new HashMap<>();
List<Integer> result = new ArrayList<>();
for (int i = 0; i < pid.size(); i++) {
List<Integer> list = map.getOrDefault(ppid.get(i), new ArrayList<>());
list.add(pid.get(i));
map.put(ppid.get(i), list);
}
List<Integer> list = new ArrayList<>();
list.add(kill);
killChildProcess(map, list, kill);
return list;
}
private void killChildProcess(Map<Integer, List<Integer>> map, List<Integer> list, int kill) {
if (map.containsKey(kill)) {
for (Integer i : map.get(kill)) {
list.add(i);
killChildProcess(map, list, i);
}
}
}
}
C++:
class Solution {
public:
/** * @param pid: the process id * @param ppid: the parent process id * @param kill: a PID you want to kill * @return: a list of PIDs of processes that will be killed in the end */
void dfs(unordered_map<int,vector<int>>& tree,int cur,vector<int>& ret){
for(auto& pid:tree[cur]){
dfs(tree,pid,ret);
}
ret.emplace_back(cur);
}
vector<int> killProcess(vector<int> &pid, vector<int> &ppid, int kill) {
// Write your code here
int n=pid.size();
vector<int> ret;
unordered_map<int,vector<int>> tree;
for(int i=0;i<n;++i){
tree[ppid[i]].emplace_back(pid[i]);
}
dfs(tree,kill,ret);
return ret;
}
};
JavaScript:
export class Solution {
/** * killProcess * * @param pid: the process id * @param ppid: the parent process id * @param kill: a PID you want to kill * @return: a list of PIDs of processes that will be killed in the end */
killProcess(pid, ppid, kill) {
// Write your code here
const cid = {
};
for(let i = 0 ; i < ppid.length ; ++i) {
const p = ppid[i];
const c = pid[i];
if(cid[p] === void 0) {
cid[p] = [c];
}else{
cid[p].push(c);
}
}
const ans = [];
const dfs = (id) => {
ans.push(id);
if(cid[id] === void 0) return;
if(cid[id]) {
for(const c of cid[id]) {
dfs(c);
}
}
}
dfs(kill);
return ans;
}
}
Go:
/** * @param pid: the process id * @param ppid: the parent process id * @param kill: a PID you want to kill * @return: a list of PIDs of processes that will be killed in the end */
import (
"sort"
)
type Arr []int
func (l Arr) Len() int {
return len(l) }
func (l Arr) Swap(i, j int) {
l[i], l[j] = l[j], l[i] }
func (l Arr) Less(i, j int) bool {
return l[i] < l[j] }
func killProcess(pid []int, ppid []int, kill int) []int {
mapParent := make(map[int][]int)
for index, value := range pid {
if ppid[index] == 0 {
continue
}
if _, ok := mapParent[ppid[index]]; !ok {
mapParent[ppid[index]] = make([]int, 0)
}
mapParent[ppid[index]] = append(mapParent[ppid[index]], value)
}
queue := make([]int, 0)
queue = append(queue, kill)
end := 0
for end < len(queue) {
for ; end < len(queue); end++ {
if _, ok := mapParent[queue[end]]; !ok {
continue
}
for _, next := range mapParent[queue[end]] {
queue = append(queue, next)
}
}
}
sort.Sort(Arr(queue))
return queue
}
Pay attention to me , Learn more about programming
边栏推荐
- Function (error prone)
- [Chongqing Guangdong education] 2408t Chinese contemporary literature reference test in autumn 2018 of the National Open University
- A application wakes up B should be a fast method
- Looking back on 2021, looking forward to 2022 | a year between CSDN and me
- 【FineBI】使用FineBI制作自定义地图过程
- Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
- PHP读取ini文件并修改内容写入
- [popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices
- [untitled]
- Threejs Internet of things, 3D visualization of farms (I)
猜你喜欢

Rome chain analysis

level18

Function (error prone)
![[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording](/img/11/b55f85ef9e1f22254218cb9083b823.png)
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording

Raki's notes on reading paper: soft gazetteers for low resource named entity recognition

Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery

Serpentine matrix

指针函数(基础)

Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent

Interview related high-frequency algorithm test site 3
随机推荐
揭秘技术 Leader 必备的七大清奇脑回路
About the prompt loading after appscan is opened: guilogic, it keeps loading and gets stuck. My personal solution. (it may be the first solution available in the whole network at present)
Convert Boolean to integer value PHP - Convert Boolean to integer value PHP
Kwai, Tiktok, video number, battle content payment
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
Longyuan war "epidemic" 2021 network security competition web easyjaba
【虚幻引擎UE】实现测绘三脚架展开动画制作
Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
快手、抖音、视频号交战内容付费
Mxnet imports various libcudarts * so、 libcuda*. So not found
Threejs Internet of things, 3D visualization of farms (II)
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
A application wakes up B should be a fast method
线上故障突突突?如何紧急诊断、排查与恢复
PHP读取ini文件并修改内容写入
Hexadecimal to decimal
PR video clip (project packaging)
A solution to the problem that variables cannot change dynamically when debugging in keil5
[untitled]
Neural networks and deep learning Chapter 2: machine learning overview reading questions