-
Notifications
You must be signed in to change notification settings - Fork 2
/
largest_plus_sign.cpp
79 lines (70 loc) · 2.15 KB
/
largest_plus_sign.cpp
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
/*
@Author - Jatin Goel
@Institute - IIIT Allahabad
Hardwork definitely pays off.
There is no substitute of hardwork.
There is no shortcut to success.
*/
class Solution {
public:
int orderOfLargestPlusSign(int N, vector<vector<int>>& mines) {
int mat[N][N];
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
mat[i][j] = 1;
}
for(int i=0;i<mines.size();i++)
mat[mines[i][0]][mines[i][1]] = 0;
int left[N][N], right[N][N], top[N][N],bottom[N][N];
for (int i = 0; i < N; i++)
{
top[0][i] = mat[0][i];
bottom[N - 1][i] = mat[N - 1][i];
left[i][0] = mat[i][0];
right[i][N - 1] = mat[i][N - 1];
}
for (int i = 0; i < N; i++)
{
for (int j = 1; j < N; j++)
{
// calculate left matrix (filled left to right)
if (mat[i][j] == 1)
left[i][j] = left[i][j - 1] + 1;
else
left[i][j] = 0;
// calculate top matrix
if (mat[j][i] == 1)
top[j][i] = top[j - 1][i] + 1;
else
top[j][i] = 0;
// calculate new value of j to calculate
// value of bottom(i, j) and right(i, j)
j = N - 1 - j;
// calculate bottom matrix
if (mat[j][i] == 1)
bottom[j][i] = bottom[j + 1][i] + 1;
else
bottom[j][i] = 0;
// calculate right matrix
if (mat[i][j] == 1)
right[i][j] = right[i][j + 1] + 1;
else
right[i][j] = 0;
// revert back to old j
j = N - 1 - j;
}
}
int n = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
int len = min(min(top[i][j], bottom[i][j]),min(left[i][j], right[i][j]));
if(len > n)
n = len;
}
}
return n;
}
};