3342 - Find Minimum Time to Reach Last Room II
info
- 문제 보기: 3342 - Find Minimum Time to Reach Last Room II
- 소요 시간: 14분 3초
- 풀이 언어:
java
- 체감 난이도: 3️⃣
- 리뷰 횟수: ✅
풀이 키워드
스포주의
다익스트라
풀이 코드
info
- 메모리: 108300 KB
- 시간: 458 ms
class Solution {
int n, m;
int[][] minTime;
int[] dy = {-1, 0, 0, 1};
int[] dx = {0, -1, 1, 0};
public int minTimeToReach(int[][] moveTime) {
this.n = moveTime.length;
this.m = moveTime[0].length;
minTime = new int[n][m];
for (int[] line : minTime) Arrays.fill(line, Integer.MAX_VALUE);
minTime[0][0] = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e[2]));
pq.add(new int[]{0, 0, 0});
while (!pq.isEmpty()) {
int[] e = pq.poll();
int y = e[0];
int x = e[1];
int t = e[2];
for (int d = 0; d < 4; ++d) {
int ny = y + dy[d];
int nx = x + dx[d];
if (ny < 0 || n <= ny || nx < 0 || m <= nx) continue;
int nt = Math.max(t + (2-(ny+nx&1)), moveTime[ny][nx] + (2-(ny+nx&1)));
if (minTime[ny][nx] <= nt) continue;
minTime[ny][nx] = nt;
pq.add(new int[]{ny, nx, nt});
}
}
return minTime[n-1][m-1];
}
}