From 4a1e18872fc9afa906f60dbdb9983a4417f9fd02 Mon Sep 17 00:00:00 2001 From: rossy0213 Date: Mon, 15 Apr 2024 02:57:50 +0900 Subject: [PATCH] add: Max Area of Island --- MaxAreaOfIsland/step1.md | 31 ++++++++++++ MaxAreaOfIsland/step2.md | 34 +++++++++++++ MaxAreaOfIsland/step3.md | 107 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 MaxAreaOfIsland/step1.md create mode 100644 MaxAreaOfIsland/step2.md create mode 100644 MaxAreaOfIsland/step3.md diff --git a/MaxAreaOfIsland/step1.md b/MaxAreaOfIsland/step1.md new file mode 100644 index 0000000..e09509e --- /dev/null +++ b/MaxAreaOfIsland/step1.md @@ -0,0 +1,31 @@ +```java +// When we find 1 in the element, we use DFS to all '1' for the same isLand and count the size of isLand. +// Then we compare the size of the island that we find to pick the biggest one. +// Time complexity: O(M*N) M: number of rows, N: number of columns +// Space complexity: O(M*N) +// Time spend: 15:30 +class Solution { + public int maxAreaOfIsland(int[][] grid) { + int max = 0; + for (int y = 0; y < grid.length; y++) { + for (int x = 0; x < grid[0].length; x++) { + if (grid[y][x] == 1) { + int size = findSizeOfIsland(grid, x, y); + max = Math.max(max, size); + } + } + } + + return max; + } + + public int findSizeOfIsland(int[][] grid, int x, int y) { + if (y < 0 || y >= grid.length || x < 0 || x >= grid[0].length || grid[y][x] != 1) { + return 0; + } + + grid[y][x] = 2; + return 1 + findSizeOfIsland(grid, x + 1, y) + findSizeOfIsland(grid, x - 1, y) + findSizeOfIsland(grid, x, y + 1) + findSizeOfIsland(grid, x, y - 1); + } +} +``` \ No newline at end of file diff --git a/MaxAreaOfIsland/step2.md b/MaxAreaOfIsland/step2.md new file mode 100644 index 0000000..34ba1df --- /dev/null +++ b/MaxAreaOfIsland/step2.md @@ -0,0 +1,34 @@ +```java +class Solution { + public int maxAreaOfIsland(int[][] grid) { + int biggestIslandSize = 0; + for (int y = 0; y < grid.length; y++) { + for (int x = 0; x < grid[0].length; x++) { + if (grid[y][x] == 1) { + int islandSize = findIslandSize(grid, x, y); + biggestIslandSize = Math.max(biggestIslandSize, islandSize); + } + } + } + + return biggestIslandSize; + } + + public int findIslandSize(int[][] grid, int x, int y) { + if (y < 0 || y >= grid.length || x < 0 || x >= grid[0].length) { + return 0; + } + + if (grid[y][x] != 1) { + return 0; + } + + grid[y][x] = 2; + + return 1 + findIslandSize(grid, x + 1, y) + + findIslandSize(grid, x - 1, y) + + findIslandSize(grid, x, y + 1) + + findIslandSize(grid, x, y - 1); + } +} +``` \ No newline at end of file diff --git a/MaxAreaOfIsland/step3.md b/MaxAreaOfIsland/step3.md new file mode 100644 index 0000000..79942ae --- /dev/null +++ b/MaxAreaOfIsland/step3.md @@ -0,0 +1,107 @@ +```java +// Time spend: 03:32 +class Solution { + public int maxAreaOfIsland(int[][] grid) { + int biggestIslandSize = 0; + + for (int y = 0; y < grid.length; y++) { + for (int x = 0; x < grid[0].length; x++) { + if (grid[y][x] == 1) { + int islandSize = findIslandSize(grid, x, y); + biggestIslandSize = Math.max(biggestIslandSize, islandSize); + } + } + } + + return biggestIslandSize; + } + + public int findIslandSize(int[][] grid, int x, int y) { + if (y < 0 || y >= grid.length || x < 0 || x >= grid[0].length) { + return 0; + } + + if (grid[y][x] != 1) { + return 0; + } + + grid[y][x] = 2; + return 1 + findIslandSize(grid, x - 1, y) + + findIslandSize(grid, x + 1, y) + + findIslandSize(grid, x, y + 1) + + findIslandSize(grid, x, y - 1); + } +} +``` + +```java +// Time spend: 03:36 +class Solution { + public int maxAreaOfIsland(int[][] grid) { + int biggestIslandSize = 0; + + for (int y = 0; y < grid.length; y++) { + for (int x = 0; x < grid[0].length; x++) { + if (grid[y][x] == 1) { + int islandSize = findIslandSize(grid, x, y); + biggestIslandSize = Math.max(biggestIslandSize, islandSize); + } + } + } + + return biggestIslandSize; + } + + public int findIslandSize(int[][] grid, int x, int y) { + if (y < 0 || y >= grid.length || x < 0 || x >= grid[0].length) { + return 0; + } + + if (grid[y][x] != 1) { + return 0; + } + + grid[y][x] = 2; + return 1 + findIslandSize(grid, x - 1, y) + + findIslandSize(grid, x + 1, y) + + findIslandSize(grid, x, y - 1) + + findIslandSize(grid, x, y + 1); + } +} +``` + +```java +// Time spend: 03:59 +class Solution { + public int maxAreaOfIsland(int[][] grid) { + int biggestIslandSize = 0; + + for (int y = 0; y < grid.length; y++) { + for (int x = 0; x < grid[0].length; x++) { + if (grid[y][x] == 1) { + int islandSize = findIslandSize(grid, x, y); + biggestIslandSize = Math.max(biggestIslandSize, islandSize); + } + } + } + + return biggestIslandSize; + } + + public int findIslandSize(int[][] grid, int x, int y) { + if (y < 0 || y >= grid.length || x < 0 || x >= grid[0].length) { + return 0; + } + + if (grid[y][x] != 1) { + return 0; + } + + grid[y][x] = 2; + return 1 + findIslandSize(grid, x - 1, y) + + findIslandSize(grid, x + 1, y) + + findIslandSize(grid, x, y + 1) + + findIslandSize(grid, x, y - 1); + } +} +``` \ No newline at end of file