-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSol2.java
More file actions
98 lines (78 loc) · 2.25 KB
/
Sol2.java
File metadata and controls
98 lines (78 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import javax.imageio.ImageTranscoder;
import java.util.Arrays;
import java.util.PriorityQueue;
public class Sol2 {
public static class Node {
int r;
int c;
int val;
Node(int r, int c, int val) {
this.r = r;
this.c = c;
this.val = val;
}
}
static final int[] DR = {-1, 1, 0, 0};
static final int[] DC = {0, 0, -1, 1};
public int solution(int n, int m, int[][] order) {
final int INF = 987654321;
int[][] tt = new int[n][m];
for (int[] row : tt) {
Arrays.fill(row, INF);
}
for (int i = 0; i <order.length; i++) {
int r = order[i][0] - 1;
int c = order[i][1] - 1;
tt[r][c] = i + 1;
}
int[][] best = new int[n][m];
for (int[] row : best) {
Arrays.fill(row, -1);
}
PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> Integer.compare(b.val, a.val));
best[0][0] = INF;
pq.add(new Node(0, 0, INF));
while (pq.isEmpty() == false) {
Node cur = pq.poll();
int r = cur.r;
int c = cur.c;
int val = cur.val;
if (val < best[r][c]) {
continue;
}
if (r == n - 1 && c == m - 1) {
return val;
}
for (int d = 0; d < 4; d++) {
int nr = r + DR[d];
int nc = c + DC[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= m) {
continue;
}
int nv = Math.min(val, tt[nr][nc]);
if (nv > best[nr][nc]) {
best[nr][nc] = nv;
pq.add(new Node(nr, nc, nv));
}
}
}
return 0;
}
public static void main(String[] args) {
Sol2 s2 = new Sol2();
int[][] arr = {
{3, 2},
{3, 1},
{1, 4},
{1, 2},
{2, 4},
{2, 3},
{2, 2},
{1, 3},
{2, 1},
{3, 3}
};
int solution = s2.solution(3, 4, arr);
System.out.println(solution);
}
}