forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0764.cpp
More file actions
59 lines (39 loc) · 1.45 KB
/
0764.cpp
File metadata and controls
59 lines (39 loc) · 1.45 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
//solved using dynamic programming
class Solution {
public:
void fillDP(vector<vector<int>> &dp, vector<vector<int>> &mat, int n){
int up, down, left, right;
for(int i=0; i<n; i++){
down = 0, right = 0;
for(int j=0; j<n; j++){
right = mat[i][j] ? right+1 : 0;
dp[i][j] = min(dp[i][j], right);
down = mat[j][i] ? down+1 : 0;
dp[j][i] = min(dp[j][i], down);
}
}
for(int i=0; i<n; i++){
up = 0, left = 0;
for(int j=n-1; j>=0; j--){
left = mat[i][j] ? left+1 : 0;
dp[i][j] = min(dp[i][j], left);
up = mat[j][i] ? up+1 : 0;
dp[j][i] = min(dp[j][i], up);
}
}
}
int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {
vector<vector<int>> dp(n, vector<int>(n, INT_MAX));
vector<vector<int>> mat(n, vector<int>(n, 1));
for(auto c : mines)
mat[c[0]][c[1]] = 0;
fillDP(dp, mat, n);
int result = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
result = max(result, dp[i][j]);
}
}
return result;
}
};