[Java] Simple Priority Queue |  Speed 98.31%
\n\n# Code\n\n    public int minimumTime(int[][] grid) {\n        if(grid[0][1] > 1 && grid[1][0] > 1) return -1;\n        int m = grid.length, n = grid[0].leng
![image.png](https://assets.leetcode.com/users/images/f388b6ed-e166-430f-a365-d4e8442e5dda_1678416705.2699091.png)\n\n# Code\n```\n    public int minimumTime(int[][] grid) {\n        if(grid[0][1] > 1 && grid[1][0] > 1) return -1;\n        int m = grid.length, n = grid[0].length;\n        int[][] directions = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n        PriorityQueue<int[]> pq = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]); \n        pq.add(new int[]{0, 0, 0});//int[step, i, j]\n        boolean[][] visited = new boolean[m][n];\n        visited[0][0] = true;\n        while(!pq.isEmpty()){\n            int[] cur = pq.poll(); //current smallest step\n            for(int[] direction : directions){\n                int i = cur[1] + direction[0];\n                int j = cur[2] + direction[1];\n                if(i < 0 || i == m || j < 0 || j == n || visited[i][j]) continue;\n                visited[i][j] = true;\n                int step = cur[0] + 1;\n                if(grid[i][j] > step) step = (grid[i][j] - step) % 2 + grid[i][j];\n                if(i == m - 1 && j == n - 1) return step;\n                pq.add(new int[]{step , i, j});\n            }     \n        }\n        return -1;\n    }\n```
['Java']