-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path764.largest-plus-sign.java
More file actions
83 lines (65 loc) · 2.05 KB
/
Copy path764.largest-plus-sign.java
File metadata and controls
83 lines (65 loc) · 2.05 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
class Solution {
// int[][] dir = new int[][]{{1,0},{-1,0},{0,1},{}};
int[][] top;
int[][] bottom;
int[][] left;
int[][] right;
int[][] grid;
public int orderOfLargestPlusSign(int n, int[][] mines) {
top = new int[n][n];
bottom = new int[n][n];
left = new int[n][n];
right = new int[n][n];
grid = new int[n][n];
for(int i=0;i<n;i++){
Arrays.fill( top[i], -1);
Arrays.fill( left[i], -1);
Arrays.fill( right[i], -1);
Arrays.fill( bottom[i], -1);
}
for(int[] mine: mines){
grid[mine[0]][mine[1]] = 1;
}
int res = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(grid[i][j] == 0){
int tp = getTop(i,j);
tp = Math.min(tp, getBot(i,j));
tp = Math.min(tp, getLft(i,j));
tp = Math.min(tp, getRgt(i,j));
res = Math.max(res, tp);
}
}
}
return res;
}
public int getTop(int i, int j ){
if(i<0 || grid[i][j] == 1)
return 0;
if(top[i][j] != -1)
return top[i][j];
return top[i][j] = 1+getTop(i-1, j);
}
public int getBot(int i, int j ){
if(j>=grid.length || grid[i][j] == 1)
return 0;
if(bottom[i][j] != -1)
return bottom[i][j];
return bottom[i][j] = 1+getBot(i, j+1);
}
public int getLft(int i, int j ){
if(j<0 || grid[i][j] == 1)
return 0;
if(left[i][j] != -1)
return left[i][j];
return left[i][j] = 1+getLft(i, j-1);
}
public int getRgt(int i, int j ){
if(i>=grid.length || grid[i][j] == 1)
return 0;
if(right[i][j] != -1)
return right[i][j];
return right[i][j] = 1+getRgt(i+1, j);
}
}