-
695. Max Area of Island javaLeetCode_Study_Plan/Algorithm 2022. 12. 22. 21:39
https://leetcode.com/problems/max-area-of-island/
class Solution { int m; int n; boolean[][] visited; int[][] grid; int cnt = 0; public int maxAreaOfIsland(int[][] grid) { this.m = grid.length; this.n = grid[0].length; this.visited = new boolean[this.m][this.n]; this.grid = grid; int max_num = 0; for (int i = 0; i < this.m; i++) { for (int j = 0; j < this.n; j++) { if (!visited[i][j] && this.grid[i][j] == 1) { this.cnt = 0; dfs(i, j); max_num = Math.max(max_num, this.cnt); } } } return max_num; } private void dfs(int i, int j) { if (go_outside(i, j)) return; if (this.visited[i][j] || this.grid[i][j] == 0) return; this.visited[i][j] = true; // System.out.println("i: " + i + " j: " + j); this.cnt++; dfs(i+1, j); dfs(i, j+1); dfs(i-1, j); dfs(i, j-1); } private boolean go_outside(int i, int j) { if (i < 0 || i >= this.m || j < 0 || j >= this.n) { return true; } return false; } }
'LeetCode_Study_Plan > Algorithm' 카테고리의 다른 글
116. Populating Next Right Pointers in Each Node (0) 2022.12.26 🎵 617. Merge Two Binary Trees java (0) 2022.12.26 733. Flood Fill java (0) 2022.12.22 3. Longest Substring Without Repeating Characters java (0) 2022.12.21 567. Permutation in String java (0) 2022.12.21