-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(leetcode): #16, study the No0695 and No0733
- Loading branch information
Showing
4 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
leetcode/src/main/java/com/github/lucaseo90/easy/No0733.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.github.lucaseo90.easy; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
public class No0733 { | ||
private final int[] dx = {1, -1, 0, 0}; | ||
private final int[] dy = {0, 0, 1, -1}; | ||
|
||
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { | ||
int n = image.length; | ||
int m = image[0].length; | ||
|
||
boolean[][] visited = new boolean[n][]; | ||
for (int i = 0; i < n; i++) { | ||
visited[i] = new boolean[m]; | ||
} | ||
|
||
Queue<Point> queue = new LinkedList<>(); | ||
queue.add(new Point(sr, sc)); | ||
|
||
while (!queue.isEmpty()) { | ||
Point point = queue.poll(); | ||
int x = point.x; | ||
int y = point.y; | ||
|
||
visited[x][y] = true; | ||
|
||
for (int i = 0; i < dx.length; i++) { | ||
int dx = x + this.dx[i]; | ||
int dy = y + this.dy[i]; | ||
|
||
if (dx >= 0 && dy >= 0 && dx < n && dy < m) { | ||
if (!visited[dx][dy] && image[x][y] == image[dx][dy]) { | ||
queue.add(new Point(dx, dy)); | ||
} | ||
} | ||
} | ||
image[x][y] = newColor; | ||
} | ||
|
||
return image; | ||
} | ||
|
||
private class Point { | ||
private int x; | ||
private int y; | ||
|
||
public Point(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
leetcode/src/main/java/com/github/lucaseo90/medium/No0695.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.github.lucaseo90.medium; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
public class No0695 { | ||
private final int[] dx = {1, -1, 0, 0}; | ||
private final int[] dy = {0, 0, 1, -1}; | ||
|
||
public int maxAreaOfIsland(int[][] grid) { | ||
int n = grid.length; | ||
int m = grid[0].length; | ||
|
||
boolean[][] visited = new boolean[n][]; | ||
for (int i = 0; i < n; i++) { | ||
visited[i] = new boolean[m]; | ||
} | ||
|
||
int largestIsland = 0; | ||
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < m; j++) { | ||
if (grid[i][j] == 1 && !visited[i][j]) { | ||
int islandSize = this.traverseIsland(grid, visited, i, j, n, m); | ||
if (largestIsland < islandSize) { | ||
largestIsland = islandSize; | ||
} | ||
} | ||
} | ||
} | ||
return largestIsland; | ||
} | ||
|
||
private int traverseIsland(int[][] grid, boolean[][] visited, int startX, int startY, int n, int m) { | ||
Queue<Point> queue = new LinkedList<>(); | ||
queue.add(new Point(startX, startY)); | ||
|
||
int size = 0; | ||
|
||
size++; | ||
visited[startX][startY] = true; | ||
|
||
while (!queue.isEmpty()) { | ||
Point point = queue.poll(); | ||
int x = point.x; | ||
int y = point.y; | ||
|
||
for (int i = 0; i < dx.length; i++) { | ||
int dx = x + this.dx[i]; | ||
int dy = y + this.dy[i]; | ||
|
||
if (dx >= 0 && dy >= 0 && dx < n && dy < m) { | ||
if (!visited[dx][dy] && grid[dx][dy] == 1) { | ||
queue.add(new Point(dx, dy)); | ||
visited[dx][dy] = true; | ||
size++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return size; | ||
} | ||
|
||
private class Point { | ||
private int x; | ||
private int y; | ||
|
||
public Point(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
leetcode/src/test/java/com/github/lucaseo90/easy/No0733Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.github.lucaseo90.easy; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
public class No0733Test { | ||
|
||
private No0733 no0733 = new No0733(); | ||
|
||
@Test | ||
public void testExample01() { | ||
Assert.assertEquals("[[2, 2, 2], [2, 2, 0], [2, 0, 1]]", (Arrays.deepToString(no0733.floodFill(new int[][]{ | ||
{1, 1, 1}, | ||
{1, 1, 0}, | ||
{1, 0, 1}, | ||
}, 1, 1, 2)))); | ||
} | ||
|
||
@Test | ||
public void testExample02() { | ||
Assert.assertEquals("[[2, 2, 2], [2, 2, 2]]", (Arrays.deepToString(no0733.floodFill(new int[][]{ | ||
{0, 0, 0}, | ||
{0, 0, 0}, | ||
}, 0, 0, 2)))); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
leetcode/src/test/java/com/github/lucaseo90/medium/No0695Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.github.lucaseo90.medium; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class No0695Test { | ||
|
||
private No0695 no0695 = new No0695(); | ||
|
||
@Test | ||
public void testExample01() { | ||
Assert.assertEquals(6, no0695.maxAreaOfIsland(new int[][]{ | ||
{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, | ||
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, | ||
{0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, | ||
{0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0}, | ||
{0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0}, | ||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, | ||
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, | ||
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0} | ||
})); | ||
} | ||
|
||
@Test | ||
public void testExample02() { | ||
Assert.assertEquals(0, no0695.maxAreaOfIsland(new int[][]{ | ||
{0, 0, 0, 0, 0, 0, 0, 0}, | ||
})); | ||
} | ||
|
||
@Test | ||
public void wrongAnswerExample01() { | ||
Assert.assertEquals(4, no0695.maxAreaOfIsland(new int[][]{ | ||
{1, 1, 0, 0, 0}, | ||
{1, 1, 0, 0, 0}, | ||
{0, 0, 0, 1, 1}, | ||
{0, 0, 0, 1, 1}, | ||
})); | ||
} | ||
} |