695. Max Area of Island
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Graphs
- Time
- —
- Space
- —
Standard Solution (C++)
SummaryGiven a binary grid of 0s (water) and 1s (land), find the area of the largest island, where an island is a group of 1s connected horizontally or vertically. Return 0 if no land exists.
Approach
- Iterate over every cell; when a land cell (1) is found, launch a DFS to explore the full connected island.
- During DFS, mark each visited land cell as 0 (sink it) to avoid revisiting, and accumulate the cell count.
- Track the maximum count seen across all island explorations.
- Sinking visited cells in-place eliminates the need for a separate visited structure.
#include <vector>
using namespace std;
class Solution {
static constexpr int dx[4] = {-1, 1, 0, 0};
static constexpr int dy[4] = {0, 0, -1, 1};
int dfs(vector<vector<int>>& grid, int r, int c) {
int m = grid.size(), n = grid[0].size();
// Out of bounds or water — contributes nothing
if (r < 0 || r >= m || c < 0 || c >= n || grid[r][c] == 0)
return 0;
grid[r][c] = 0; // sink to prevent revisiting
int area = 1;
for (int k = 0; k < 4; ++k)
area += dfs(grid, r + dx[k], c + dy[k]);
return area;
}
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int maxArea = 0;
for (int i = 0; i < (int)grid.size(); ++i)
for (int j = 0; j < (int)grid[0].size(); ++j)
if (grid[i][j] == 1)
maxArea = max(maxArea, dfs(grid, i, j));
return maxArea;
}
}; Time O(m * n) Space O(m * n)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
#include <vector>
#include <queue>
using namespace std;
class Solution {
public:
// void dfs(vector<vector<int>>& grid, int& curArea, int x, int y){
// if(x<0 || x>=grid.size() || y<0 || y>=grid[0].size() || grid[x][y]==0){
// return;
// }
// ++curArea;
// grid[x][y]=0;
// for(int i=0; i<4; ++i){
// dfs(grid, curArea, x+dx[i], y+dy[i]);
// }
// }
int maxAreaOfIsland(vector<vector<int>>& grid) {
int m=grid.size();
int n=grid[0].size();
int maxArea=0;
queue<pair<int, int>> q;
const int dx[4]={-1, 1, 0, 0};
const int dy[4]={0, 0, -1, 1};
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
if(grid[i][j]==1){
int curArea=0;
// bfs
q.push({i, j});
grid[i][j]=0;
while(!q.empty()){
// int curLen=q.size();
// for(int i=0; i<curLen; ++i){
auto [x, y]=q.front();
q.pop();
++curArea;
for(int k=0; k<4; ++k){
int nx=x+dx[k];
int ny=y+dy[k];
if(nx<0 || nx>=m || ny<0 || ny>=n || grid[nx][ny]==0){
continue;
}
q.push({nx, ny});
grid[nx][ny]=0;
}
// }
}
maxArea=max(maxArea, curArea);
}
}
}
return maxArea;
}
}; My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.