Simple JAVA Solution | Greedy | Dijkstras Algorithm | Graph
\n# Complexity\n- Time complexity: O(nm log(nm))\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n*m)\n Add your space complexity here, e.g
\n# Complexity\n- Time complexity: O(n*m log(n*m))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n*m)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int minimumTime(int[][] grid) {\n        \n        if(grid[0][1]>=2 && grid[1][0]>=2) return -1;\n        int[][] dir = new int[][]{{0,1},{1,0},{-1,0},{0,-1}};\n        int n = grid.length,m=grid[0].length;\n        boolean vis[][] = new boolean[n][m];\n        PriorityQueue<int[]> q = new PriorityQueue<>((a,b)-> a[0]-b[0]);\n        q.add(new int[]{0,0,0});\n        while(!q.isEmpty())\n        {\n            int[] cur = q.poll();\n            int t = cur[0],r = cur[1],c = cur[2];\n            if(r==n-1 && c==m-1) return t;\n            if(vis[r][c]) continue;\n            vis[r][c]=true;\n\n            for(int[] i:dir)\n            {\n                int nr = r + i[0],nc = c + i[1];\n                if(nr<0 || nr>=n || nc<0 || nc >=m || vis[nr][nc]) continue;\n                int cycle = ((grid[nr][nc] - t) % 2 ==0) ? 1 : 0;\n                q.add(new int[]{Math.max(grid[nr][nc]+cycle,t+1),nr,nc});\n            }\n        }\n        return -1;\n    }\n}\n```
['Array', 'Breadth-First Search', 'Graph', 'Heap (Priority Queue)', 'Matrix', 'Shortest Path', 'Java']