-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_Matrix.cpp
51 lines (44 loc) · 1.32 KB
/
12_Matrix.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
// https://practice.geeksforgeeks.org/problems/largest-square-in-a-binary-matrix-with-at-most-k-1s-for-multiple-queries
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> largestSquare(vector<vector<int>> M, int R, int C, int K, int Q, int q_i[], int q_j[])
{
vector<vector<int>> dp(R + 1, vector<int>(C + 1));
for (int i = 1; i <= R; i++)
for (int j = 1; j <= C; j++)
dp[i][j] = M[i - 1][j - 1] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
vector<int> ans(Q);
for (int q = 0; q < Q; q++)
{
int x = q_i[q] + 1, y = q_j[q] + 1, val = M[x - 1][y - 1], side = 1;
while (x - side >= 0 and y - side >= 0 and x <= R and y <= C)
{
val = dp[x][y] + dp[x - side][y - side] - dp[x - side][y] - dp[x][y - side];
if (val <= K) ans[q] = side;
else break;
x += 1;
y += 1;
side += 2;
}
}
return ans;
}
};
class Solution
{
public:
vector<int> largestSquare(vector<vector<int>> M, int R, int C, int K, int Q, int q_i[], int q_j[])
{
// code here
K = ceil(sqrt(K));
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}