Why Swift Code Does not work?


  • 0
    A

    Was wondering if someone with better knowledge in Swift than me tell me why the code below does not work. It's essentially identical to the top sol in Java but for whatever reason it is timing out. I'm suspecting the sort funct, but the sort funct in Swift runs in O(nlogn) time also.

    func findMinArrowShots(_ points: [[Int]]) -> Int {
        var points = points
        if points.count == 0 { return 0 }
        points.sort { $0[1] < $1[1] }
        var currentPos = points[0][1]
        var count = 1
        for i in 1 ..< points.count {
            if currentPos >= points[i][0] {
                continue
            } else {
                count += 1
                currentPos = points[i][1]
            }
        }
        
        return count
    }
    

Log in to reply
 

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