Swift solution got TLE


  • 1
    F

    Can anyone please point out what the issue is?

    func numberOfBoomerangs(_ points: [[Int]]) -> Int {
             if points.count == 0 {
                 return 0
             }
             var dictionary = [Int: Int]()
             var res = 0
             for p in points {
                 for q in points {
                    let x = p[0] - q[0]
                    let y = p[1] - q[1]
                    let distance =  x * x + y * y
                    dictionary[distance] = (dictionary[distance] ?? 0 ) + 1
                    res +=  2 * (dictionary[distance]! - 1)
                 }
                 dictionary.removeAll()
             }
            return res
        }
    

  • 0
    C

    @fiona8957 I think the time measurements might be wrong for Swift. My solution is similar to yours and all the published solutions, but I'm still getting TLE.

    In your case, one improvement to try is getting rid of the dictionary.removeAll() call. Instead, just create a new empty dictionary for every iteration through the outer for loop.


Log in to reply
 

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