Swift | Dijkstra
Code\nswift []\nimport Collections\n\nstruct Node: Comparable {\n    var row: Int\n    var col: Int\n    var time: Int\n\n    static func < (lhs: Node, rhs: Nod
# Code\n```swift []\nimport Collections\n\nstruct Node: Comparable {\n    var row: Int\n    var col: Int\n    var time: Int\n\n    static func < (lhs: Node, rhs: Node) -> Bool {\n        lhs.time < rhs.time\n    }\n}\n\nclass Solution {\n    func minimumTime(_ grid: [[Int]]) -> Int {\n        guard grid[0][1] <= 1 || grid[1][0] <= 1 else { return -1 }\n\n        let (m, n) = (grid.count, grid.first?.count ?? 0)\n        var visited = Array(repeating: Array(repeating: false, count: n), count: m)\n        var heap = Heap([Node(row: 0, col: 0, time: 0)])\n\n        while let currentNode = heap.popMin() {\n            if (currentNode.row, currentNode.col) == (m - 1, n - 1) { return currentNode.time }\n            if visited[currentNode.row][currentNode.col] { continue }\n\n            for (dr, dc) in [(1, 0), (-1, 0), (0, 1), (0, -1)] {\n                let (nextRow, nextCol) = (currentNode.row + dr, currentNode.col + dc)\n                if !(0..<m).contains(nextRow) || !(0..<n).contains(nextCol) { continue }\n                let waitTime = (grid[nextRow][nextCol] - currentNode.time) % 2 == 0 ? 1 : 0\n                let nextTime = max(grid[nextRow][nextCol] + waitTime, currentNode.time + 1)\n                heap.insert(Node(row: nextRow, col: nextCol, time: nextTime))\n            }\n\n            visited[currentNode.row][currentNode.col] = true\n        }\n        return -1\n    }\n}\n```
['Swift']