Skip to content

Commit

Permalink
Create 1937. Maximum Number of Points with Cost1 (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Aug 28, 2024
2 parents 8517c0b + 4b71838 commit c3df2c6
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 1937. Maximum Number of Points with Cost1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
int n; //row
int m; //column
bool flag;
void dfs(vector<vector<int>>& grid1, vector<vector<int>>& grid2,int row,int column){
if (row < 0 || row >= n || column < 0 || column >= m || grid2[row][column] == 0) {
return;
}
grid2[row][column] = 0; // mark visited
dfs(grid1, grid2, row - 1, column); // up
dfs(grid1, grid2, row + 1, column); // down
dfs(grid1, grid2, row, column - 1); // left
dfs(grid1, grid2, row, column + 1); // right
if (grid1[row][column] == 0) {
flag = false; // no island found
}
}
int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
n = grid1.size();
m = grid1[0].size();
int countSubIslands = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid2[i][j] == 1) {
flag = true;
dfs(grid1, grid2, i, j);
countSubIslands += flag;
}
}
}
return countSubIslands;
}
};

0 comments on commit c3df2c6

Please sign in to comment.