반응형
https://leetcode.com/problems/max-area-of-island/
class Solution {
public:
int d[5] = {0,1,0,-1,0};
bool v[50][50] = {false};
int maxAreaOfIsland(vector<vector<int>>& grid) {
int n = grid.size(), m = grid.front().size();
int res = 0;
for(int i=0; i<n; i++) {
for(int j=0;j<m; j++) {
if(grid[i][j] && !v[i][j])
res = max(res, dfs(grid, n, m, i, j));
}
}
return res;
}
int dfs(vector<vector<int>>& grid, int n, int m, int x, int y) {
v[x][y] = true;
int s = 1;
for(int i=0; i<4; i++) {
int nx = x + d[i];
int ny = y + d[i + 1];
if(nx < 0 || ny < 0 || nx >= n || ny >= m || !grid[nx][ny] || v[nx][ny]) continue;
s += dfs(grid, n, m, nx, ny);
}
return s;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 1220 : Count Vowels Permutation (0) | 2022.08.07 |
---|---|
LeetCode 576 : Out of Boundary Paths (0) | 2022.07.16 |
LeetCode 746 : Min Cost Climbing Stairs (0) | 2022.07.10 |
LeetCode 1696 : Jump Game VI (0) | 2022.07.09 |
LeetCode 1465 : Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts (0) | 2022.07.02 |