Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions MaxAreaOfIsland/step1.md
Original file line number Diff line number Diff line change
@@ -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);
}
}
```
34 changes: 34 additions & 0 deletions MaxAreaOfIsland/step2.md
Original file line number Diff line number Diff line change
@@ -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);
}
}
```
107 changes: 107 additions & 0 deletions MaxAreaOfIsland/step3.md
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私は、0 > y || y >= grid.length の順ですね。
0 <= y && y < grid.length の逆だと分かりやすいからです。

return 0;
}

if (grid[y][x] != 1) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grid[y][x] なのってなんかこだわりあります?
横縞か縦縞かですが。
(VRAM を感じているんですが。)

Copy link
Copy Markdown
Owner Author

@rossy0213 rossy0213 Apr 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

単純に自分にとってこの方がイメージしやすくコード書きやすいからです。

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);
}
}
```