golang code time "Time Limit Exceeded" why, some one help out?


  • 0

    thankkks

    func killProcess(pid []int, ppid []int, kill int) []int {
    	if len(pid) == 0 {
    		return nil
    	}
    
    	var exist bool
    	m := make(map[int][]int)
    	for i := 0; i < len(pid); i++ {
    		pp, p := ppid[i], pid[i]
    		m[pp] = append(m[pp], p)
    
    		if p == kill {
    			exist = true
    		}
    	}
    	if !exist {
    		return nil
    	}
    
    	if _, ok := m[kill]; !ok {
    		return []int{kill}
    	}
    
    	var r []int
    	q := list.New()
    	q.PushBack(kill)
    	for q.Len() > 0 {
    		h := q.Front()
    		q.Remove(h)
    		hn := h.Value.(int)
    
    		r = append(r, hn)
    
    		if _, ok := m[hn]; ok {
    			for _, v := range m[hn] {
    				q.PushBack(v)
    			}
    		}
    	}
    	return r
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.