Java Clean Solution - 3/11/2024
Complexity\n- Time complexity:O(nm)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(nm*2)\n Add your space complexity here, e.g. O(n) \n\n# 
# Complexity\n- Time complexity:$$O(n*m)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n*m*2)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Pair implements Comparable<Pair> {\n    int x;\n    int y;\n    int parity;\n    int time;\n\n    public Pair(int x, int y, int time, int parity) {\n        this.x = x;\n        this.y = y;\n        this.parity = parity;\n        this.time = time;\n    }\n\n    @Override\n    public int compareTo(Pair p) {\n        return Integer.compare(this.time, p.time);\n    }\n}\n\nclass Solution {\n    int[][] dirs = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };\n\n    public int minTimeToReach(int[][] moveTime) {\n        int n = moveTime.length, m = moveTime[0].length;\n\n        int[][][] dist = new int[n][m][2];\n        for (int[][] d : dist) {\n            for (int i = 0; i < m; i++) {\n                d[i][0] = Integer.MAX_VALUE;\n                d[i][1] = Integer.MAX_VALUE;\n            }\n        }\n\n        PriorityQueue<Pair> pq = new PriorityQueue<>();\n        pq.add(new Pair(0, 0, 0, 0));\n        dist[0][0][0] = 0;\n\n        while (!pq.isEmpty()) {\n            Pair top = pq.poll();\n            int x = top.x, y = top.y;\n            int time = top.time;\n            int par = top.parity;\n\n            if (time > dist[x][y][par]) {\n                continue;\n            }\n\n            for (int[] d : dirs) {\n                int nx = x + d[0], ny = y + d[1];\n\n                if (nx >= 0 && nx < n && ny >= 0 && ny < m) {\n                    int nt;\n                    if (par == 0) {\n                        nt = 1 + Math.max(time, moveTime[nx][ny]);\n                    } else {\n                        nt = 2 + Math.max(time, moveTime[nx][ny]);\n                    }\n\n                    if (nt < dist[nx][ny][1 - par]) {\n                        dist[nx][ny][1 - par] = nt;\n                        pq.offer(new Pair(nx, ny, nt, 1 - par));\n                    }\n                }\n            }\n        }\n        return Math.min(dist[n - 1][m - 1][0], dist[n - 1][m - 1][1]);\n    }\n}\n```
['Array', 'Math', 'Dynamic Programming', 'Queue', 'Heap (Priority Queue)', 'Java']