golang code Time Limit Exceeded


  • 0
    B
    package main
    
    import "fmt"
    
    func killProcess(pid []int, ppid []int, kill int) []int {
    	parent_tree := make(map[int][]int)
    	visited := make(map[int]int)
    
    	for _, p := range pid {
    		parent_tree[p] = []int{}
    		visited[p] = 0
    	}
    
    	for i, p := range ppid {
    		parent_tree[p] = append(parent_tree[p], pid[i])
    	}
    	var result []int
    	killAll(visited, parent_tree, kill, &result)
    	return result
    }
    
    func killAll(visited map[int]int, parent_tree map[int][]int, kill int, result *[]int) {
    	*result = append(*result, kill)
    	visited[kill] = 1
    	for _, p := range parent_tree[kill] {
    		if visited[p] == 0 {
    			visited[p] = 1
    			killAll(visited, parent_tree, p, result)
    		}
    	}
    }
    
    func main() {
    	pid := make([]int, 50000)
    	ppid := make([]int, 50000)
    	for i := 0; i < 50000; i++ {
    		pid[i] = i + 1
    		ppid[i] = 1
    	}
    	ppid[0] = 0
    	kill := 1
    	fmt.Println(killProcess(pid, ppid, kill))
    }
    

    It takes 30 ms in my computer. But submit with Time Limit Exceeded error. Is there any stack memory limit?


  • 0

    @beyondkmp

    hey there, I met the same problem, golang, you have a solution already?


Log in to reply
 

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