[Python] Hash map + BFS TLE?


  • 0
    T

    Can anyone explain why this results in TLE?

    class Solution(object):
        def visit(self, kill, m):
            ans = []
            queue = m.get(kill) # { 3: [1, 5], 5: [10, 20]}
            
            while queue:
                current = queue.pop(0)
                ans.append(current)
                if current in m:
                    queue += m[current]
                    
            return ans
        
        def killProcess(self, pid, ppid, kill):
            m = {}
            for child_id, parent_id in zip(pid, ppid):
                m[parent_id] = m.get(parent_id, []) + [child_id]
                
            res = [kill]
            descendents = self.visit(kill, m)
            res += descendents
            
            return res      
    

Log in to reply
 

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