Skip to content

Commit db99a19

Browse files
authored
Create find-all-groups-of-farmland.cpp
1 parent 52953c5 commit db99a19

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

C++/find-all-groups-of-farmland.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Time: O(m * n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<vector<int>> findFarmland(vector<vector<int>>& land) {
7+
vector<vector<int>> result;
8+
for (int i = 0; i < size(land); ++i) {
9+
for (int j = 0; j < size(land[0]); ++j) {
10+
if (land[i][j] != 1) {
11+
continue;
12+
}
13+
int ni = i, nj = j;
14+
while (ni + 1 < size(land) && land[ni + 1][j]) {
15+
++ni;
16+
}
17+
while (nj + 1 < size(land[0]) && land[i][nj + 1]) {
18+
++nj;
19+
}
20+
for (int r = i; r <= ni; ++r) {
21+
for (int c = j; c <= nj; ++c) {
22+
land[r][c] = -1;
23+
}
24+
}
25+
result.push_back({i, j, ni, nj});
26+
}
27+
}
28+
return result;
29+
}
30+
};

0 commit comments

Comments
 (0)