Scala Time limit exceeded


  • 0
    W

    I'm trying to do this problem in Scala, but I'm getting a time limit exceeded for the first test case. When I run it it solves it fine.

    import collection.mutable.HashMap
    import collection.mutable.Queue
    object Solution {
        def killProcess(pid: List[Int], ppid: List[Int], kill: Int): List[Int] = {
            val hashMap = new HashMap[Int, List[Int]]
            for (i <- 0 until pid.length) {
                val temp = hashMap.getOrElse(ppid(i),  List[Int]())
                hashMap(ppid(i)) = pid(i) :: temp
            }
            val q = new Queue[Int]
            q.enqueue(kill)
            var res = List[Int]()
            while(!q.isEmpty) {
                val temp = q.dequeue
                res = temp :: res
                hashMap.getOrElse(temp, List[Int]()).foreach {(i: Int) => q.enqueue(i)}
                
            }
            res
        }
    }
    

    Is this just a scala issue?


Log in to reply
 

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