695. Max Area of Island
View on LeetCode ↗
C++
class Solution {public: int DFS(vector<vector<int>>& grid, int x, int y, int size) { if (x < 0 || y < 0 || x >= grid.size() || y >= grid[0].size() || grid[x][y] == 0) return size; grid[x][y] = 0; return 1 + DFS(grid, x + 1, y, size) + DFS(grid, x - 1, y, size) + DFS(grid, x, y + 1, size) + DFS(grid, x, y - 1, size); } int maxAreaOfIsland(vector<vector<int>>& grid) { // loop through each cell, x,y and run DFS on each but for each // successful run, add one to the island size. and double for loop // here will do the maxing strat int best = 0; for (int i = 0; i < grid.size(); i++) { for (int j = 0; j < grid[0].size(); j++) { int result = DFS(grid, i, j, 0); best = max(result, best); } } return best; }};
light work, just do dfs, edit the grid, make checks as needed for statement, and then exit by returning the size of each island.